MVC Web.Net: intercept all calls before reaching the controller?

I have a .Net MVC web application (Not WebAPI) and I want to intercept all calls in the web application before they reach the controller, check the value in the request headers and do something if the value isn 't ( e.g., view 404). What is the perfect way to do this? Keep in mind that this is not a web API application, just a simple web application.

+6
source share
2 answers

Depending on what exactly you want to do, you can use the default controller, which is extended by all other controllers. This way you can override OnActionExecuting or Initialize and do your check there.

 public class ApplicationController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { //do your stuff here } } public class YourController : ApplicationController { } 
+5
source

You are looking for global action filters.

Create a class that inherits ActionFilterAttribute , overrides OnActionExecuting() to do your processing, and adds instances to the global filter collection in Global.asax.cs (inside RegisterGlobalFilters() )

+3
source

All Articles