简体   繁体   中英

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. What is the replacement for GlobalFilterCollection in Net Core?

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. Follow these steps:

In your project, under Dependencies in your solution explorer, right click and choose Add Reference option. Then under the Reference Manager window, click on Browse.

Then navigate where your ASP .NET is installed. It should be here:

Program files(x86) --> Microsoft ASP.NET --> ASP.NET MVC 4.6 --> Assemblies---> 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; and then you would be able to use GlobalFilterCollection filters in your project.

EDIT :

In .NET CORE, the way your register your filters has changed. 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

For example: If I want to use AuthorizeAttribute, I cannot add AuthorizeAttribute into MvcOptions.Filters . I would need to create an AuthorizationPolicy and use AuthorizeFilter .

var authorizepolicy= new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .RequireRole("Admin", "SuperUser")
        .Build();

services.AddMvc(options =>
{
    options.Filters.Add(new AuthorizeFilter(authorizepolicy));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM