I have a controller that is all ajax calls, so I want to verify it's an ajax call once on the page before it calls the controller action so that if it's not an ajax call I can redirect to a home or error page. Is this possible? I know I can put in the constructor class, but the request object is null at that point.
views:
20answers:
1
A:
You can use an ActionFilter
. It's designed to accomplish this task:
public class MyActionFilterAttribute : ActionFilterAttribute {
public override void OnActionExecuting(ActionExecutingContext context) {
// will get executed when the decorated action runs
}
}
// Action method:
[MyActionFilter]
public ActionResult Index(int id) {
// ...
}
Mehrdad Afshari
2009-12-30 05:36:18
If I wanted to add a bit of logic even before the controller is called, is that possible?
Jhorra
2009-12-30 05:49:50
It's possible to dive in the ASP.NET pipeline by creating an HTTP module provide additional logic but it doesn't make sense for most ASP.NET MVC applications. If you think you require that kind of functionality, you should try to redesign unless you really have to deal with something at a lower level (like manipulating authentication and session state behavior provided by ASP.NET).
Mehrdad Afshari
2009-12-30 06:05:18