MVC 3依赖注入与Ninject 2.2 +全球行动过滤器 [英] MVC 3 Dependency Injection with Ninject 2.2 + Global Action Filter
问题描述
我想使用ASP.NET MVC 3和Ninject 2.2注入Logger对象到自定义ActionFilterAttribute。
I am trying to use ASP.NET MVC 3 and Ninject 2.2 to inject a logger object into a custom ActionFilterAttribute.
我能够得到这个,如果我有标记的自定义属性每个控制器工作。
I am able to get this to work if I mark each controller with the custom attribute.
不过,我不能得到这个,如果我从控制器删除该属性的装饰,并尝试使用全球行动过滤器的工作。
However I cannot get this to work if I remove the attribute decoration from the controllers and try to use a global action filter.
下面是code:
下App_Start - NinjectMVC3.cs
using NinjectTest.Abstract;
using NinjectTest.Concrete;
[assembly: WebActivator.PreApplicationStartMethod(typeof(NinjectTest.App_Start.NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(NinjectTest.App_Start.NinjectMVC3), "Stop")]
namespace NinjectTest.App_Start
{
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Mvc;
public static class NinjectMVC3
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ILogger>().To<Log4NetLogger>();
}
}
}
的Global.asax.cs
using System.Web.Routing;
using NinjectTest.Attributes;
namespace NinjectTest
{
public class MvcApplication : HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new TestLoggingAttribute());
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
TestLoggingAttribute.cs
using System.Web.Mvc;
using Ninject;
using NinjectTest.Abstract;
namespace NinjectTest.Attributes
{
public class TestLoggingAttribute : ActionFilterAttribute
{
[Inject]
public ILogger _logger { get; set; }
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var controller = filterContext.RouteData.Values["controller"];
var action = filterContext.RouteData.Values["action"];
_logger.Info("controller: " + controller + " action: " + action);
base.OnActionExecuted(filterContext);
}
}
}
HomeController.cs
using System.Web.Mvc;
using NinjectTest.Attributes;
namespace NinjectTest.Controllers
{
//[TestLogging]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}
}
就像我提到的,如果我在控制器这一切工作的注释[TestLogging]属性。不过,我想用一个全球性的滤镜代替。
有人可以帮我吗?
Like I mentioned if I uncomment the [TestLogging] attribute in the controller this all works. However I want to use a global filter instead. Can someone help me out?
推荐答案
您可能会发现<一个href=\"http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/\">following博客帖子有用。因此,在你的 RegisterServices
方法简单:
You might find the following blog post useful. So in your RegisterServices
method simply:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ILogger>().To<Log4NetLogger>();
kernel.BindFilter<TestLoggingAttribute>(FilterScope.Global, 0);
}
和使 BindFilter
扩展方法为范围不要忘了添加适当的使用
指令:
and to bring the BindFilter
extension method into scope don't forget to add a proper using
directive:
using Ninject.Web.Mvc.FilterBindingSyntax;
这篇关于MVC 3依赖注入与Ninject 2.2 +全球行动过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!