我对ActionFilterAttriute有一个问题,它不能正确地重定向。我不太熟悉完整的代码基,但我已经看到了足够多的东西,无法理解到底发生了什么。
为了简化代码,我删除了不相关的部分:
public class ResolveApplicationRedirectAttribute : ActionFilterAttribute
{
//some variables
private ActionExecutingContext _filterContext;
protected string ApplicationRedirectUrl
{
get { return ConfigurationManager.AppSettings["ApplicationRedirectUrl"]; }
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_filterContext = filterContext;
//a lot of logic that decide if we should redirect or not
//I added this after the other logic to make it always redirect regardless of what happens inside the logic above
_filterContext.Result = new RedirectResult(ApplicationRedirectResult);
}
}
[ResolveApplicationRedirect]
public ActionResult Index(CrmQueryStringParameters crmParameters){
//some logic
}这通常是可行的,但是当应用程序在短时间内被几个请求击中时,Index方法就会被调用,因为View丢失了一些数据(我们知道它丢失了一些数据)。这就是我们想重定向的原因)。
但是现在,当我添加_filterContext.Result = new RedirectResult(ApplicationRedirectResult)作为OnActionExecuting方法的最后一行时,它怎么可能仍然调用我的Action?
是否有任何已知的bug/角落案例/其他任何东西可以导致MVC无视我已放入filterContext中的filterContext,并且无论如何都会触发操作方法?
即使我将OnActionExecuting设置为最后一行,也可能在filterContext.Result逻辑中出现任何可能导致问题的特殊情况。属性中的任何异常都应该将其炸毁,而不是跳过属性并调用Action方法。
如果你能帮我指出正确的方向,我将不胜感激。
发布于 2013-08-26 08:30:41
我终于发现了这个问题。它与使用_filterContext类变量有关。
问题是MVC 不为每个请求创建过滤器的新实例,从而导致多个请求共享属性的相同实例。
有关更多详细信息,请参见打破更改列表在MVC3发行说明中。
发布于 2013-08-26 04:46:35
+1表示Wouter的结果。考虑到您所展示的代码,不应该实现您所描述的结果。也许// magic logic here正在返回一个空_filterContext.Result
https://stackoverflow.com/questions/18402526
复制相似问题