问题描述
我有一个BaseController
,其中通过覆盖OnActionExecuting
在ViewData
集合中放入了一些数据.
I have a BaseController
in which I put in some data in the ViewData
collection by overriding OnActionExecuting
.
现在,我在ChildController
中有一个不需要该视图数据的动作.
Now i have an Action in a ChildController
that doesn't need that view data.
为此,我创建了一个DontPopulateViewData
ActionFilterAttribute,该属性在BaseController
上设置了一个布尔值,以防止BaseController
填充视图数据.
For that purpose I created an DontPopulateViewData
ActionFilterAttribute that sets a bool on the BaseController
that prevents the BaseController
from populating the viewdata.
问题:ActionFilters OnActionExecuting
方法是在BaseController
中的方法之后而不是之前被调用的.
Problem: the ActionFilters OnActionExecuting
method is called after the one in BaseController
and not before.
在基本控制器中重写OnActionExecuting
之前,总是会调用ActionFilters吗,有没有办法解决这个问题?
Will ActionFilters always be called before overridden OnActionExecuting
in base controllers and is there a way to get around this?
推荐答案
除了Marwan Aouida发表和建议的内容(在基类上使用ActionFilter)之外,我认为您将无法创建在基类的OnActionExecuting()重载之前执行的ActionFilter.以下代码:
In addition to what Marwan Aouida posted and suggested (using an ActionFilter on the base class), I don't think you're going to be able to create an ActionFilter that executes before the OnActionExecuting() overload on the base class. The following code:
[MyActionFilter(Name = "Base", Order = 2)]
public class MyBaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
Response.Write("MyBaseController::OnActionExecuting()<br>");
base.OnActionExecuting(filterContext);
}
protected override void Execute(System.Web.Routing.RequestContext requestContext)
{
requestContext.HttpContext.Response.Write("MyBaseController::Execute()<br>");
base.Execute(requestContext);
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
Response.Write("MyBaseController::OnActionExecuted()<br>");
base.OnActionExecuted(filterContext);
}
}
public class MyActionFilter : ActionFilterAttribute
{
public string Name;
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("MyActionFilter_" + Name + "::OnActionExecuted()<br>");
base.OnActionExecuted(filterContext);
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("MyActionFilter_" + Name + "::OnActionExecuting()<br>");
base.OnActionExecuting(filterContext);
}
}
public class MyTestController : MyBaseController
{
[MyActionFilter(Name = "Derived", Order = 1)]
public void Index()
{
Response.Write("MyTestController::Index()<br>");
}
}
产生以下输出:
MyBaseController::Execute()
MyBaseController::OnActionExecuting()
MyActionFilter_Derived::OnActionExecuting()
MyActionFilter_Base::OnActionExecuting()
MyTestController::Index()
MyActionFilter_Base::OnActionExecuted()
MyActionFilter_Derived::OnActionExecuted()
MyBaseController::OnActionExecuted()
这篇关于在BaseController的OnActionExecuting之前调用FilterAttribute的OnActionExecuting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!