![](/img/trans.png)
[英]The type or namespace name 'GlobalFilterCollection' could not be found (are you missing a using directive or an assembly reference?)
[英]Net Core: Type or namespace name 'GlobalFilterCollection' could not be found
我正在将一个项目从.Net 4.6.2迁移到.Net Core 2.0。 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)
为此,您将明确需要在项目中添加System.Web.Mvc.dll
。 跟着这些步骤:
在您的项目中,在解决方案资源管理器中的“ Dependencies
下,右键单击并选择“ Add Reference
选项。 然后在“ Reference Manager
窗口下,单击“浏览”。
然后导航您的ASP .NET的安装位置。 应该在这里:
程序文件(x86)-> Microsoft ASP.NET-> ASP.NET MVC 4.6->程序集---> System.Web.Mvc.dll
将此文件包含在项目的程序集中。 包含此.dll文件后,只需using System.Web.Mvc;
导入即可using System.Web.Mvc;
然后您将可以在项目中使用GlobalFilterCollection filters
。
编辑 :
在.NET CORE中,注册过滤器的方式已更改。 我将仅向您展示一个有关如何注册过滤器的小例子。
根据官方文件 :
您可以通过将其添加到Startup类的ConfigureServices方法中的MvcOptions.Filters集合中来全局注册一个过滤器(针对所有控制器和操作)。
例如:如果要使用AuthorizeAttribute,则无法将AuthorizeAttribute
添加到MvcOptions.Filters
。 我需要创建一个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.