开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜