How to access configuration values from appsettings.json file inside ValidationAttribute class?

From this article, you will gain a knowledge on how to access appsettings.json properties or configuration values inside Custom Validation Attribute class.

To achieve this, First We should have a custom validation attribute class that implements ValidationAttribute class.

Add Override IsValid method inside our custom validator class.

Please find the sample code snippet for this scenario,

We are going to write a unit test for the line no’s 10 and 12 using MOQ framework.

The following code has IConfiguration and IServiceProvider interfaces because these two are dependencies to our CustomValidator class. All these are mocking using MOQ framework.

It’s your turn now to use the above code snippet and pass your own values and test it. I believe this article helps you to solve your problem and saves your valuable time.

— Happy Learning




I love sharing things what I know to others and passionate in photography.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to send and receive messages between AWS Lambda and Amazon SQS

Analysis of Prim’s Algorithm — A Blog

Software Engineering Interview Preparation (Emergency Kit)

Cpanel Web-site Internet hosting It is really Simple to Use

Configuring Azure Workers in a Jenkins Hybrid Cloud (Ansible)

Kubernetes, distributed operating systems, and language-level parallelism.

The Basics of Breakpoints you Might not Know

Duplication in an Array II

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nuthan Murarysetty

Nuthan Murarysetty

I love sharing things what I know to others and passionate in photography.

More from Medium

How to Unit Test Attributes are Applied to MVC Web API Controllers

The best way to analyze .Net assemblies without Assembly.LoadFile(), beyond ReflectionOnlyLoad

Setting Up Minimal.Net6 Rest API with SQL Server.

Write a code to retrieve data from table into a datagridview in CSharp