Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One method is to remove your logic from the Attribute and implement it in a class that implements IActionFilter. The class is then registered with the container so that dependency injection will work correctly. Orchard CMS using this approach.</p> <pre><code>public class MyCustomActionFilterAttribute : Attribute { } public class MyCustomActionFilter : FilterProvider, IActionFilter { protected MyService Service { get; private set; } // MyService can be injected by the container... public MyCustomActionFilter(MyService service) { this.Service = service; } public void OnActionExecuted(ActionExecutedContext filterContext) { // Check to see if the action has a matching attribute var attributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MyCustomActionFilterAttribute), true); // Perform some logic here.... } public void OnActionExecuting(ActionExecutingContext filterContext) { } } </code></pre> <p>It is possible to create an IActionInvoker that applies the filter to the action, this class is automatically instantiated my MVC using the DependencyResolver. </p> <pre><code>public class FilterResolvingActionInvoker : ControllerActionInvoker { protected IEnumerable&lt;IFilterProvider&gt; Providers { get; private set; } // Filters registered with the container are injected by the container public FilterResolvingActionInvoker(IEnumerable&lt;IFilterProvider&gt; providers) { this.Providers = providers; } // Add the filter to the current FilterInfo protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) { var filters = base.GetFilters(controllerContext, actionDescriptor); foreach (var provider in this.Providers) { provider.AddFilters(filters); } return filters; } } </code></pre> <p>Define a common interface that allows us to register our filters.</p> <pre><code>public interface IFilterProvider { void AddFilters(FilterInfo filterInfo); } public abstract class FilterProvider : IFilterProvider { public void AddFilters(FilterInfo filterInfo) { if (this is IActionFilter) { filterInfo.ActionFilters.Add(this as IActionFilter); } } } </code></pre> <p>And register them with the container builder. It is also possible to create an extension method for Autofac to automatically register all IFilterProviders in your assembly.</p> <pre><code>builder.RegisterType&lt;FilterResolvingActionInvoker&gt;().As&lt;IActionInvoker&gt;().InstancePerDependency(); builder.RegisterType&lt;MyCustomActionFilter&gt;().As&lt;IFilterProvider&gt;().InstancePerDependency(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload