[英]Net Core: Type or namespace name 'GlobalFilterCollection' could not be found
I am migrating a project from .Net 4.6.2 into .Net Core 2.0. 我正在将一个项目从.Net 4.6.2迁移到.Net Core 2.0。 What is the replacement for GlobalFilterCollection in Net Core?
Net Core中GlobalFilterCollection的替代品是什么?
The type or namespace name 'GlobalFilterCollection' could not be found (are you missing a using directive or an assembly reference?) HPE.Kruta.Web.Core
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
In order to do this, you would explicitly need to add the System.Web.Mvc.dll
in your project. 为此,您将明确需要在项目中添加
System.Web.Mvc.dll
。 Follow these steps: 跟着这些步骤:
In your project, under Dependencies
in your solution explorer, right click and choose Add Reference
option. 在您的项目中,在解决方案资源管理器中的“
Dependencies
下,右键单击并选择“ Add Reference
选项。 Then under the Reference Manager
window, click on Browse. 然后在“
Reference Manager
窗口下,单击“浏览”。
Then navigate where your ASP .NET is installed. 然后导航您的ASP .NET的安装位置。 It should be here:
应该在这里:
Program files(x86) --> Microsoft ASP.NET --> ASP.NET MVC 4.6 --> Assemblies---> System.Web.Mvc.dll 程序文件(x86)-> Microsoft ASP.NET-> ASP.NET MVC 4.6->程序集---> System.Web.Mvc.dll
Include this file in your Assemblies in your project. 将此文件包含在项目的程序集中。 Once you include this .dll file, simply import
using System.Web.Mvc;
包含此.dll文件后,只需
using System.Web.Mvc;
导入即可using System.Web.Mvc;
and then you would be able to use GlobalFilterCollection filters
in your project. 然后您将可以在项目中使用
GlobalFilterCollection filters
。
EDIT : 编辑 :
In .NET CORE, the way your register your filters has changed. 在.NET CORE中,注册过滤器的方式已更改。 I will just show you a small example on how you can register a filter.
我将仅向您展示一个有关如何注册过滤器的小例子。
As per official documentation : 根据官方文件 :
You can register a filter globally (for all controllers and actions) by adding it to the MvcOptions.Filters collection in the ConfigureServices method in the Startup class
您可以通过将其添加到Startup类的ConfigureServices方法中的MvcOptions.Filters集合中来全局注册一个过滤器(针对所有控制器和操作)。
For example: If I want to use AuthorizeAttribute, I cannot add AuthorizeAttribute
into MvcOptions.Filters
. 例如:如果要使用AuthorizeAttribute,则无法将
AuthorizeAttribute
添加到MvcOptions.Filters
。 I would need to create an AuthorizationPolicy
and use AuthorizeFilter
. 我需要创建一个
AuthorizationPolicy
并使用AuthorizeFilter
。
var authorizepolicy= new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.RequireRole("Admin", "SuperUser")
.Build();
services.AddMvc(options =>
{
options.Filters.Add(new AuthorizeFilter(authorizepolicy));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.