Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register global filters in ASP.Net MVC 4 and Autofac

I have a filter like this one:

public class CustomFilterAttribute : ActionFilterAttribute, IAuthorizationFilter {     public MyPropery Property { get; set; }     .... } 

I need it to be run for every actions in my project

I tried to register it in the GlobalFilters but my property doesn't get injected

I tried This solution to register my filter but it doesn't get called

Any idea on how to do that?

like image 872
JuChom Avatar asked Nov 12 '12 15:11

JuChom


People also ask

How do I register a global filter?

You can apply filters at a global level in the Application_Start event of the global. asax. cs file by using default FilterConfig. RegisterGlobalFilters() method.

How do I register a global filter in Web API?

To apply the filter to all Web API controllers, add it to GlobalConfiguration. Filters. public static class WebApiConfig { public static void Register(HttpConfiguration config) { config. Filters.

What is global filter in MVC?

Generally, in asp.net mvc global action filters are mainly used for exception/error handling and logging exceptions. In asp.net mvc, we can apply global action filters to all action methods in our application.


1 Answers

There's a new way of registering MVC global filters in AutoFac. First, remove the filter registration from your RegisterGlobalFilters because we will have Autofac handle adding them to our controllers/actions instead of MVC.

Then, register your container as follows:

var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly());  builder.RegisterType<MyProperty>().As<IProperty>();  builder.Register(c => new CustomFilterAttribute(c.Resolve<IProperty>()))                 .AsActionFilterFor<Controller>().InstancePerHttpRequest();  builder.RegisterFilterProvider();  IContainer container = builder.Build();  DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 

Note that by passing in the Controller class into the extension AsActionFilterFor() we are telling AutoFac to apply this filter to all classes that derive from the Controller class (which, in MVC, is all controllers). Since we are calling AsActionFilterFor() without any arguments, we are also specifying we want to have the filter applied to all actions within the specified controllers. If you want to apply your filter to a select controller and action, you can use lambda expressions like so:

builder.Register(c => new CustomFilterAttribute(c.Resolve<IProperty>()))     .AsActionFilterFor<HomeController>(c => c.Index())     .InstancePerHttpRequest(); 

If your action takes a parameter, use the default keyword to specify:

builder.Register(c => new CustomFilterAttribute(c.Resolve<IProperty>()))     .AsActionFilterFor<HomeController>(c => c.Detail(default(int)))     .InstancePerRequest(); 

Note that you have to use a different extension method based on what type of filter you are registering, here are the supported filter types:

  • AsActionFilterFor
  • AsAuthorizationFilterFor
  • AsExceptionFilterFor
  • AsResultFilterFor
like image 136
Pete Avatar answered Oct 02 '22 11:10

Pete