开发者

Is there a method or constructor that can be called prior to the requested action?

开发者 https://www.devze.com 2022-12-15 05:42 出处:网络
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 ca

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) {
   // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消