asp.net-mvc 如何在 ASP.NET MVC 2 中的每个控制器操作之前执行某些代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2650269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get certain code to execute before every single controller action in ASP.NET MVC 2?
提问by Chris
I want to check some things about the state of the session, the user agent, etc, and possibly take action and return a special view BEFORE a controller method gets a chance to execute. For example:
我想检查一些有关会话状态、用户代理等的信息,并可能在控制器方法有机会执行之前采取行动并返回一个特殊视图。例如:
Most common:
User requests Home/Index
System checks to make sure x != 0.
x does not equal zero, so the Home/Index controller executes like normal.
最常见:
用户请求 Home/Index
系统检查以确保 x != 0.
x 不等于 0,因此 Home/Index 控制器正常执行。
But, sometimes:
User requests Home/Index
System checks to make sure x != 0.
x DOES equal zero. The user must be notified and the requested controller action cannot be allowed to execute.
但是,有时:
用户请求主页/索引
系统检查以确保 x != 0.
x DOES 等于零。必须通知用户并且不允许执行请求的控制器操作。
I thinkthis involves the use of ActionFilters. But I have read about them and I don't understand if I can preempt the controller method and return a view before it executes. I am sure I could execute code before the controller method runs, but how do I keep it from running in some instances and return a custom view, or direct to a different controller method?
我认为这涉及到 ActionFilters 的使用。但是我已经阅读了它们,我不明白我是否可以抢占控制器方法并在它执行之前返回一个视图。我确信我可以在控制器方法运行之前执行代码,但是如何防止它在某些情况下运行并返回自定义视图,或直接到不同的控制器方法?
Update: I implemented RM's solution. This is what I did:
更新:我实施了 RM 的解决方案。这就是我所做的:
public class MyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (myValue == wrongValue)
{
filterContext.Result = new ViewResult{ViewName = "Notice"};
}
base.OnActionExecuting(filterContext);
}
}
Now, when myValue is wrong, those users get the Notice view and the requested controller is never executed. To make this work I applied it to a ControllerBase that all my Controllers inherit from.
现在,当 myValue 错误时,这些用户将获得 Notice 视图,并且永远不会执行请求的控制器。为了完成这项工作,我将它应用到我的所有控制器都继承自的 ControllerBase。
回答by Rosstified
All depends what exactly you want to do, and how. Three options below:
一切都取决于您究竟想做什么,以及如何做。以下三个选项:
You can use route constraints for this. They are executed when evaluating the route to match to.
您可以为此使用路由约束。它们在评估要匹配的路由时执行。
routes.MapRoute(
"HomeWithConstraint",
"Home/{action}",
new {controller="Home", action="index"},
new { x = new MyCustomRouteConstraint () }
);
// without constraint, i.e. if above didnt pass
routes.MapRoute(
"HomeWithConstraint",
"Home/{action}",
new {controller="Home", action="index"}
);
The MyCustomRouteConstraint type above would check for x==0 etc in your example. Not sure exactly what you want to do, but this will allow you to check conditions before running and set additional route values etc.
上面的 MyCustomRouteConstraint 类型将在您的示例中检查 x==0 等。不确定您想要做什么,但这将允许您在运行之前检查条件并设置其他路由值等。
See herefor example of custom route constraints.
有关自定义路由约束的示例,请参见此处。
Alternativly, yes you can use a custom ActionFilter, just apply it to the controller class, and it will be called before any action is executed. Something like:
或者,是的,您可以使用自定义 ActionFilter,只需将其应用于控制器类,它将在执行任何操作之前被调用。就像是:
public class CheckXActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if(x == 0)
{
// do something
// e.g. Set ActionParameters etc
}
else
{
// do something else
}
}
}
Another option is to have all you controllers (or the relevant ones) inherit from a custom controller you make, and override :
另一种选择是让您所有的控制器(或相关的)从您制作的自定义控制器继承,并覆盖:
OnActionExecuting
See herefor details.
有关详细信息,请参见此处。
To do the same as the filter, or routing constraints.
做同样的过滤,或者路由约束。
回答by Hussein Shabbir
One way you can do this is to redirect to a different ActionMethod to show the view. Code example is in this discussion:
执行此操作的一种方法是重定向到不同的 ActionMethod 以显示视图。代码示例在此讨论中:
Redirecting to specified controller and action in asp.net mvc action filter