2013-01-11 60 views
0

有人可以为我澄清这种情况:MVC管道如何工作?

  1. 我们要求例如Home \ Index;
  2. 在全球ASAX我们Application_AuthorizeRequest
  3. Application_AuthorizeRequest抛出异常
  4. 我们的Application_Error它捕捉到它并返回新的View

    一个IController控制器=新ErrorController(); // routedata ok controller.Execute(new RequestContext(new HttpContextWrapper(Context),routeData));

  5. 与错误操作是执行(这是确定的)

  6. 但随后MVC或ASP管道,依然试图执行首页\指数,我怎样才能使管道忘了要求?

据我了解MVC它是HttpHandler,我怎样才能确保我的错误行动是所有这些链中的最后一步?

回答

1

此设置存在问题。如果你想防止被调用,你应该写一个自定义的授权指数action属性,而不是使用Authenticate_Request事件:

public class MyAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     // perform the logic you were doing in your Authenticate_Request 
     // here to authorize the user. You could throw exceptions as well 
     throw new Exception("ok"); 
    } 
} 

授权过滤器更换Authenticate_Request方法在ASP.NET MVC应用程序,这就是你应该是什么使用。

,然后装饰与此属性的索引操作:

public class HomeController: Controller 
{ 
    [MyAuthorize] 
    public ActionResult Index() 
    { 
     ...  
    } 
} 

现在你的Application_Error将被调用,执行错误控制和索引行动从来没有被触发,正是因为它应该是。