在 ASP.Net MVC 4 和 Autofac 中注册全局过滤器 [英] Register global filters in ASP.Net MVC 4 and Autofac

查看:35
本文介绍了在 ASP.Net MVC 4 和 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

我尝试在 GlobalFilters 中注册它,但我的属性没有被注入

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

知道怎么做吗?

推荐答案

有一种在 AutoFac 中注册 MVC 全局过滤器的新方法.首先,从您的 RegisterGlobalFilters删除过滤器注册,因为我们将让 Autofac 处理将它们添加到我们的控制器/操作而不是 MVC.

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

请注意,通过将 Controller 类传递到扩展 AsActionFilterFor() 中,我们告诉 AutoFac 将此过滤器应用于从 Controller 派生的所有类 类(在 MVC 中,它是所有控制器).由于我们在调用 AsActionFilterFor() 时不带任何参数,因此我们还指定我们希望将过滤器应用于指定控制器中的所有操作.如果您想将过滤器应用于选择的控制器和操作,您可以像这样使用 lambda 表达式:

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

如果您的操作需要一个参数,请使用 default 关键字来指定:

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

这篇关于在 ASP.Net MVC 4 和 Autofac 中注册全局过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆