ASP.NET Core – Client-side custom validation attributes

I wrote about how to add custom validation attributes. These are used for model validation on the server-side. You can also use these for client-side validation, which I’ll show in this article. 1 – Implement IClientModelValidator The first step is to implement the IClientModelValidator interface in the custom validation attribute class. This has a single … Read more

C# – How to disable ModelStateInvalidFilter

There are two options for disabling ModelStateInvalidFilter: You’d do this when you want to manually perform model validation. I’ll show both options below. Disable ModelStateInvalidFilter globally To disable ModelStateInvalidFilter for *all* actions, set SuppressModelStateInvalidFilter=true in ConfigureApiBehaviorOptions, like this: Disable ModelStateInvalidFilter for specific actions To disable ModelStateInvalidFilter selectively, you can implement an IActionModelConvention attribute that removes … Read more

C# – How to unit test a model validation attribute

You can unit test a validation attribute by creating an instance of it and then testing the two methods: In this article, I’ll show examples of unit testing these methods in a custom validation attribute and in a built-in validation attribute (i.e. [Range]). Unit testing a custom validation attribute Consider the following custom validation attribute … Read more

ASP.NET Core – Create a custom model validation attribute

There are many built-in model validation attributes available – such as [Required] and [Range] – which you can use to handle most validation scenarios. When these aren’t sufficient, you can create a custom validation attribute with your own validation logic. I’ll show an example of how to do that. 1 – Subclass ValidationAttribute and implement … Read more

C# – How to read the Description attribute

You can use the Description attribute to describe types and type members (properties, methods). One of the most common use cases is providing a user-friendly string for enum values. Here’s an example of using the Description attribute with an enum: To read the Description attribute, use reflection and do the following steps: This can be … Read more

C# – How to use JsonConverterAttribute

You can use JsonConverterAttribute (from System.Text.Json) to apply a specific JsonConverter to a property. Apply this attribute on a property and specify the JsonConverter type to use, like this: In this example, it’s applying ExpirationDateConverter (a custom JSON converter) to handle the ExpirationDate. For reference, here’s ExpirationDateConverter’s definition: Now serialize the object to JSON: Here’s … Read more

ASP.NET Core – How to unit test an action filter

To unit test an action filter, you have to pass in an action filter context object (which requires a lot of setup). Action filter methods are void, so you have to verify the behavior by inspecting the context object (or dependencies, like a logger, if you are injecting those). Here’s an example of doing the … Read more

Add a custom action filter in ASP.NET Core

Action filters allow you to look at requests right before they are routed to an action method (and responses right after they are returned from the action method). The simplest way to add your own action filter in ASP.NET Core is to subclass ActionFilterAttribute and then override the appropriate methods depending on if you want … Read more

Feature flags in ASP.NET Core

In this article, I’ll show an example of using a feature flag to toggle an endpoint on/off with a configuration value in appsettings.json. 1 – Install feature management package Install the Microsoft.FeatureManagement.AspNetCore package. Note: This is using (View > Other Windows > Package Manager Console). 2 – Add feature flags to appsettings.json Edit appsettings.json and … Read more

C# – Using custom attributes

Attributes are used to store additional info about a class/method/property. The attributes are read at runtime and used to change the program’s behavior. Here are a few examples of commonly used built-in attributes: In general, you should try to use built-in attributes when possible. When it makes sense, you can create your own custom attribute. … Read more