I have a .Net MVC web application (Not WebAPI), and I want to intercept all calls to the web app before they reach the controller, check for a value in the request headers, and do something if the value isn't present (such as presenting a 404). What's the ideal way to do this? Keep in mind this is not a Web API application, just a simple web application.
Asked
Active
Viewed 3,831 times
7
-
1possible duplicate of [ASP.NET MVC 4 intercept all incoming requests](http://stackoverflow.com/questions/11726848/asp-net-mvc-4-intercept-all-incoming-requests) – George Johnston Apr 17 '13 at 19:36
-
After you check the value, are you going to pass the request to the controller? – Floremin Apr 17 '13 at 19:36
2 Answers
6
Depending on what specifically you want to do, you could use a default controller which all other controllers extend. That 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
{
}

Mansfield
- 14,445
- 18
- 76
- 112
3
You're looking for global action filters.
Create a class that inherits ActionFilterAttribute
, override OnActionExecuting()
to perform your processing, and add an instances to global filter collection in Global.asax.cs (inside RegisterGlobalFilters()
)

SLaks
- 868,454
- 176
- 1,908
- 1,964
-
Using this approach, can you specify for which routes do you want to do this? – Luis Deras Sep 22 '16 at 19:29