Ninject和MVC3:依赖注入动作过滤器 [英] Ninject and MVC3: Dependency injection to action filters
本文介绍了Ninject和MVC3:依赖注入动作过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现如何做到使用Ninject在ASP.NET MVC3的ActionFilter财产注入尚无定论的文章和问题的负载。
I've found loads of inconclusive articles and questions on how to do property injection on an ActionFilter in ASP.NET MVC3 using Ninject.
可能有人给我一个明显的例子吗?
Could someone give me a clear example please?
下面是我的自定义AUTH属性。
Here's my custom auth attribute.
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
[Inject]
public IService Service { get; set; }
[Inject]
public IAuthenticationHelper AuthenticationHelper { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
//My custom code
}
}
我现在用的是WebActivator设立Ninject
I am using the WebActivator to set up Ninject
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.Web.AppStart_NinjectMvc3), "Start")]
namespace MyProject.Web {
public static class AppStart_NinjectMvc3 {
public static void RegisterServices(IKernel kernel) {
//Binding things
}
public static void Start() {
// Create Ninject DI Kernel
IKernel kernel = new StandardKernel();
// Register services with our Ninject DI Container
RegisterServices(kernel);
// Tell ASP.NET MVC 3 to use our Ninject DI Container
DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
}
}
}
我的服务和帮助从来没有注入。我需要做什么改变?
My service and helper are never injected. What do I need to change?
推荐答案
下面是你如何才能够着手:
Here's how you could proceed:
public class MvcApplication : Ninject.Web.Mvc.NinjectHttpApplication
{
private class MyModule : NinjectModule
{
public override void Load()
{
Bind<IService>().To<ServiceImpl>();
Bind<IAuthenticationHelper>().To<AuthenticationHelperImpl>();
}
}
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
protected override IKernel CreateKernel()
{
var modules = new INinjectModule[] {
new MyModule()
};
var kernel = new StandardKernel(modules);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
return kernel;
}
}
然后你可以有你的自定义属性授权:
and then you could have your custom authorize attribute:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
[Inject]
public IService Service { get; set; }
[Inject]
public IAuthenticationHelper AuthenticationHelper { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
}
}
和一个控制器动作饰吧:
and a controller action decorated with it:
[CustomAuthorize]
public ActionResult Index()
{
return View();
}
和依赖应注射。
这篇关于Ninject和MVC3:依赖注入动作过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文