在ASP.NET MVC的全球行动过滤器 [英] Global action filter in ASP.NET MVC

查看:118
本文介绍了在ASP.NET MVC的全球行动过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能建立一个全球行动过滤器会自动适用于所有的动作在ASP.NET MVC应用程序的所有控制器?我想是这样on Rails的的before_filter在Ruby中的ApplicationController中定义的。

Is it possible to create a global action filter that will automatically apply to all actions in all controllers in ASP.NET MVC application? I want something like "before_filter" defined in ApplicationController in Ruby on Rails.

感谢您的帮助。

推荐答案

这真的取决于你想用它做什么。在许多情况下,通过vucetica和阿迪尔的previous答案将是你真正想做的事。但是,他们都没有遇见你所列出的标准:自动适用于所有动作/控制器

This really depends what you want to do with it. In many scenarios, the previous answers by vucetica and Adeel will be what you actually want to do. However, neither of them meet the criteria you listed: automatically apply to all actions/controllers.

要那样做,你就需要实现在Global.asax中的应用BeginRequest事件的处理程序。请参阅 MSDN文档了解详情。

To do something like that, you would need to implement a handler for the Application BeginRequest event in Global.asax. See the MSDN documentation for more information.

更新 - 2010年7月27日的:的 ScottGu的博客上讲述 MVC 3 preVIEW 1,其中包括全局过滤器就像你在谈论的框架。他们通过Global.asax中注册,并可以应用到所有控制器或基于特定的标准。

Update - July 27, 2010: ScottGu blogged about MVC 3 Preview 1, which includes a framework for global filters like you're talking about. They're registered via Global.asax, and can apply to all controllers or based on specific criteria.

这篇关于在ASP.NET MVC的全球行动过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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