我如何获得一定的code在ASP.NET MVC 2的每一个控制器动作之前执行? [英] How do I get certain code to execute before every single controller action in ASP.NET MVC 2?

查看:142
本文介绍了我如何获得一定的code在ASP.NET MVC 2的每一个控制器动作之前执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查一些事情有关会话,用户代理等的状态,并可能采取的行动,并返回一个特殊的视图之前控制器方法得到执行的机会。例如:

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:

最常见的:结果
用户请求主页/搜索指数
系统检查,以确保到X!= 0结果
X不等于零,所以家庭/索引控制器执行像正常的。

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.

不过,有时:结果
用户请求主页/搜索指数
系统检查,以确保到X!= 0结果
点¯xDOES等于零。用户必须得到通知,并请求控制器动作不能被允许执行。

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.

我的认为的这涉及到使用ActionFilters的。但是我看了一下他们,我不明白,如果我可以preempt控制器方法,它执行前返回一个视图。我确信控制器方法运行之前,我可以执行code,但如何保持它在某些情况下运行,并返回一个自定义视图,或直接到不同控制器的方法?

I think this 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?

更新:我实现了RM的解决方案。这就是我所做的:

Update: I implemented RM's solution. This is what I did:

public class MyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (myValue == wrongValue)
        {
            filterContext.Result = new ViewResult{ViewName = "Notice"};
        }
        base.OnActionExecuting(filterContext);
    }
}

现在,当myvalue的是错的,这些用户得到通知视图,并永远不会执行请求的控制器。为了使这项工作我应用它,我所有的控制器继承一个ControllerBase。

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.

推荐答案

一切都取决于正是你想要做什么,以及如何。下面有三个选项:

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"}
);

以上MyCustomRouteConstraint类型会为您在您的实例Ⅹ== 0等。不知道正是你想要做什么,但是这将允许您检查运行前的条件和设置其他路由值等。

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.

请参阅这里例如自定义路由约束。

See here for example of custom route constraints.

Alternativly,是的,你可以使用自定义ActionFilter,只是把它应用到控制器类,并执行任何操作之前将被调用。是这样的:

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:

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

请参阅here了解详情。

要做到一样的过滤器或路由限制。

To do the same as the filter, or routing constraints.

这篇关于我如何获得一定的code在ASP.NET MVC 2的每一个控制器动作之前执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆