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

查看:32
本文介绍了如何在 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天全站免登陆