简体   繁体   中英

Filter .net core for all controller except one

I use .NET Core 3.1 and have one filter which I want to use in all controller except one.

I do not want to use [attribute] on all controllers.

I need just a way to say a specific controller does not use the filter.

You can implement filter globally set something like this on your FilterConfig.

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews(options =>
    {
        options.Filters.Add(typeof(MySampleActionFilter));
    });
}

Then you can check if the current controller is the one executing on your CustomFilter.

    public class CustomActionFilter : ActionFilterAttribute, IActionFilter
    {
         void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
         {
              if(!filterContext.ActionDescriptor.ControllerDescriptor.ControllerName == "ExemptedController") {
                  OnActionExecuting(filterContext);
              }
         }
    }

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