I have read the similar questionWhat is the purpose of RegisterGlobalFilter
but unable to get the answer, the question somewhat revolves around some other stuff too and the anwser doesn't seems fullfilling to me.
My question is:- what is the purpose of this line inside global.asax in MVC 5 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
The FilterConfig
is a custom class in your code, normally under the App_Start folder and generally looks somewhat like this:
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } }
You can add custom filters to this list that should be executed on each request. If you inherit from the FilterAttribute
class or one of its inheritors you can create your own filters, for instance a log filter.
You can also apply these filters to controllers that requires certain constraints. For instance, if you add the [RequireHttps]
filter attribute (example below) to a controller or a method in your controller, the user must use a https request in order to execute the code in the method. So instead of handling it in each method, the filter takes care of it.
[RequireHttps] public class MyController : ApiController { // only https requests will get through to this method. [HttpGet] public IHttpActionResult Get() { return Ok(); } }
You can think of it as a little box that sits between the users browser and your controller and filters out any invalid requests, or one the executes when a controller is done and you need to postprocess the result to the user.
If you want to read more, msdn has more details regarding filters at Filtering in ASP.NET MVC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With