Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a number of .NET Core web API Controllers that will need a validate header method.

It will do the same thing on each controller. I would like to learn how to properly design this.

What I have tried:

Nothing yet, my 'go to' option is to create a 'technicalClass' and have the Validate method on it, then each controller instantiates this technicalClass to use the Validate method. But that is an absolutely awful approach. This class does not represent anything and is just a placeholder for methods like this.

I'm thinking maybe extend the controller base class, any other suggestions?
Posted
Updated 7-Feb-20 0:30am
v2

 
Share this answer
 
Comments
Ger Hayden 7-Feb-20 6:46am    
That would make an excellent learning exercise.
What you are looking for basically is known as a Filter. A filter is a piece of utility code that can run immediately before or after a controller action and is used for things such as authorization, caching, or logging.

A similar type of utility code is known as Middleware. Middleware is made up of the first and last piece of your code that a request receives, and can perform much of the same functions as a filter.
Middleware fills many of the same roles as HttpModules did in earlier versions of ASP.NET (not Core).

Both of the above can "short-circuit" a request and send a response without being processed by a controller.

The key difference between Filters and Middleware is that Middleware is applied to ALL requests while Filters are applied at the Controller or Action level.
This is the choice you have to decide on- IF all of your requests need to have a HeaderValidation then you go with Middleware. If only half of your requests need to go through this then you go with a Filter.
The "grey area" is if only a few requests don't need it, you could write a "special exclusion" into Middleware for specific requests as needed.

Filters in ASP.NET Core | Microsoft Docs[^]
Exploring Middleware as MVC Filters in ASP.NET Core 1.1[^]
Middleware vs Filters ASP. NET Core - The .NET Cultist[^]
 
Share this answer
 
Comments
Ger Hayden 7-Feb-20 6:49am    
Never thought of filters - this action will apply to all post requests, and is the first hurdle any post must pass, otherwise it is rejected. Perfect for a filter.
MadMyche 7-Feb-20 7:24am    
If it's going to be 100% of the POST requests and 0% of the rest, I would consider doing this as Middleware; this should be an easy to maintain exclusion to the validation rule
Ger Hayden 7-Feb-20 9:27am    
That will be the case. If I have to show my code to a prospective employer, it would look much better for this advice.
Extending the controller base class with validating method could be an option. If you make it virtual, you could even override it for more specific needs eventually. But that means that you will have to change all your controllers and make them inherit from extended controller.

On the other hand, you could create a static validation method in a helper class. This would discharge you from having to instantiate it everytime you want to validate.
 
Share this answer
 
Comments
Ger Hayden 7-Feb-20 4:51am    
If I continue down the helper class route, I will take the instantiation overhead rather than static.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900