如何在ASP.NET MVC中的所有操作中安装操作筛选器? [英] How to install an action filter in all actions in ASP.NET MVC?

查看:72
本文介绍了如何在ASP.NET MVC中的所有操作中安装操作筛选器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法设置动作过滤器,例如

Is there a way to have an action filter, like

public class MyActionFilterAttribute : ActionFilterAttribute {
    public override void OnActionExecuting(ActionExecutingContext context) {
    ...

会自动应用于网站中的所有操作吗?

be automatically applied to all actions in a web site?

推荐答案

我不认为有现成的方法可以做到这一点.对于简单站点,最简单的操作是在控制器级别应用过滤器.这很普遍,通常最好有一个自己的基本控制器类,以防诸如此类的事情出现在您想要传播到所有控制器的地方.例如:

I don't believe there is an out-of-the-box way to do this. The easiest thing to do for simple sites is just apply the filter at the Controller level. This is pretty common, and generally it's a good idea to have your own base controller class in case things like this crop up where you want to propagate it to all your controllers. E.g.:

[MyActionFilter]
public class MyBaseController : Controller
{
  ...
}

public class HomeController : MyBaseController
{
  ...
}

话虽这么说,

That being said, here is a blog post showing how you can achieve application wide action filters. Looks like a small amount of work, but perhaps you can use this technique.

这篇关于如何在ASP.NET MVC中的所有操作中安装操作筛选器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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