[英]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.