C# 如何使用StructureMap将属性注入自定义ActionFilterAttribute?
注射在我的解决方案中正常工作,但customs Action FilterAttribute除外。C# 如何使用StructureMap将属性注入自定义ActionFilterAttribute?,c#,asp.net-mvc-4,structuremap,custom-action-filter,C#,Asp.net Mvc 4,Structuremap,Custom Action Filter,注射在我的解决方案中正常工作,但customs Action FilterAttribute除外。 下面是一个示例:我想使用注入到以下ActionFilterAttribute中来填充属性UserModel public class UserFilterAttribute : ActionFilterAttribute { public IUserModel UserModel { get; set; } public override void OnActionExecuti
下面是一个示例:我想使用注入到以下ActionFilterAttribute中来填充属性UserModel
public class UserFilterAttribute : ActionFilterAttribute
{
public IUserModel UserModel { get; set; }
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
// here is my problem : this.UserModel is always null
base.OnActionExecuting(actionContext);
}
}
Global.asax.cs:
private void ConfigureDependencies()
{
Guid userId = new Guid();
// Register models
IContainer container = new Container(cfg =>
{
cfg.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
cfg.For<IFilterProvider>().Use<StructureMapFilterProvider>();
cfg.For<IUserModel>().Use<UserModel>().Ctor<Guid>().Is(userId);
cfg.SetAllProperties(x => { x.OfType<IUserModel>(); });
});
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory(container));
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
}
protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
this.ConfigureDependencies();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
}
尝试将
IController
添加到扫描仪中
cfg.Scan(扫描=>
{
扫描。卡入总成();
scan.WithDefaultConventions();
scan.AddAllTypesOf();
});
我的控制器中的注入已起作用。但是,我“以防万一”测试了您的建议,但它没有改变任何东西。您看过吗?在public IUserModel UserModel{get;set;}
上设置[SetterProperty]
(structuremap property injector)属性。
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new UserFilterAttribute());
}
}
cfg.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IController>();
});