开发者

ASP.NET MVC action filters: Setting the filterContext.Result in a filter prevents other filters from being executed?

I currently have 2 filters, Auth and Redirect that do the following: Filter Auth, who implements IAuthorizationFilter and ActionFilter, checks for user login and authorization, and if that fails sets the filterContext.Result to be a HttpStatusCodeResult of 403 (forbidden). Filter Redirect, who implements IActionFilter and ActionFilter, checks the Result and if it's a 403, redirects to a login page.

I have them applied to an action as follows:

[Auth(Order=0)]
[Redirect(Order=1)]

However, Auth gets executed but Redirect never gets executed (not one of the 4 overrideable methods it provides). If I remove Auth Redirect gets executed, but if I include Auth as the first filter, Redirect isn't executed. I guess that setting the Result property of the filter context prevents any other filters from getting executed, but I can't realiza why this happens. FYI I'm using ASP.NET MVC 3 beta, but that shouldn't change anything.

Update: Changing the filter type of Auth to IActionFilter instead of IAuthorizationFilter causes the OnResultExecuting and OnResultExecuted in Redirect to fire, but changing the Response there has no effect whatsoever开发者_JS百科 on the final response to the browser.


I ended up fixing it by making Redirect a IResultFilter and writing the following in OnResultExecuting:

filterContext.HttpContext.Response.Redirect(url);


When you set HttpStatusCodeResult you are setting a redirect, so the second filter won't execute (because you already have redirected).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜