如何使用 mvc 6、asp.net 5 注册全局过滤器 [英] How to register a global filter with mvc 6, asp.net 5
问题描述
我正在尝试在一个简单的 mvc 应用程序中注册一个过滤器.除了基本过滤器之外,我还没有真正写过任何东西来测试.我正在使用 VS 2015 RC,我通过转到新项目 -> Asp.net Web 应用程序 -> Web API 创建了初始应用程序.我遇到的问题是我找不到全局注册过滤器的方法.
I'm trying to register a filter in a simple mvc application. I haven't even really written anything yet outside of the basic filter to test with. I'm using VS 2015 RC and I created the initial application by going to new project -> Asp.net Web Application -> Web API. The problem I'm having is that I can't find a way to register the filter globally.
在早期版本的 MVC 中,我看到了 GlobalFilters.Filters
,但是当我尝试在新框架中使用它时,它告诉我找不到 GlobalFilters.在以前的版本中,它位于 System.Web.MVC 中,但我不再在我的参考文献中看到它,而且我似乎无法在任何地方找到它.
From earlier versions of MVC I see the GlobalFilters.Filters
, but when I try to use that in the new framework it tells me that GlobalFilters can't be found. In previous versions it lived in System.Web.MVC, but I no longer see that in my references, and I can't seem to find it anywhere.
这看起来应该很简单,但到目前为止我还没有找到方法.
This seems like it should be very simple but so far I haven't found a way to do it.
这是我的 project.json
Here's my project.json
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-beta4",
"Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta4"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
},
"frameworks": {
"dnx451": {
"frameworkAssemblies": {
}
},
"dnxcore50": { }
},
"exclude": [
"wwwroot",
"node_modules",
"bower_components"
],
"publishExclude": [
"node_modules",
"bower_components",
"**.xproj",
"**.user",
"**.vspscc"
]
}
推荐答案
如何在 MVC 6 中做到这一点的示例
Example of how you can do it in MVC 6
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.ConfigureMvc(options =>
{
options.Filters.Add(new YouGlobalActionFilter());
}
}
这篇关于如何使用 mvc 6、asp.net 5 注册全局过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!