shayan kamalzadeh
shayan kamalzadeh

Reputation: 124

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.

Upvotes: 2

Views: 1508

Answers (1)

tontonsevilla
tontonsevilla

Reputation: 2799

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

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

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);
              }
         }
    }

Upvotes: 1

Related Questions