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 cal开发者_StackOverflow社区l 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.
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) {
// ...
}
精彩评论