ASP.NET MVC 3 and Global Filtering

Hello, I am trying to implement a global filter with injection. The filter is as follows.

public class WikiFilter : IActionFilter { private IWikiService service; public WikiFilter(IWikiService service) { this.service = service; } public void OnActionExecuting(ActionExecutingContext filterContext) { !!!Code here!! } public void OnActionExecuted(ActionExecutedContext filterContext) { throw new NotImplementedException(); } } 

And I connected the injection filter as follows in my global.asax.

  public class MvcApplication : System.Web.HttpApplication, IAuthenticationApplication<User> { protected void Application_Start() { Ninject(); AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); RegisterGlobalFilters(GlobalFilters.Filters); } private void Ninject() { // Create Ninject DI kernel IKernel kernel = new StandardKernel(); kernel.Bind<DataContext>().ToSelf().InRequestScope(); kernel.Bind<IWikiRepository>().To<WikiRepository>(); kernel.Bind<IWikiService>().To<WikiService>(); // Global filters kernel.BindFilter<WikiFilter>(FilterScope.Global, 0); DependencyResolver.SetResolver (new NinjectDependencyResolver(kernel)); } } 

But for some reason, the filter never starts when the application starts, I did not implement it correctly?

+6
source share
1 answer

I would recommend that you use the ~/App_Start/NinjectMVC3.cs to configure the Ninject kernel:

 [assembly: WebActivator.PreApplicationStartMethod(typeof(AppName.App_Start.NinjectMVC3), "Start")] [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(AppName.App_Start.NinjectMVC3), "Stop")] namespace AppName.App_Start { using System.Web.Mvc; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Ninject.Web.Mvc; using Ninject.Web.Mvc.FilterBindingSyntax; 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<DataContext>().ToSelf().InRequestScope(); kernel.Bind<IWikiRepository>().To<WikiRepository>(); kernel.Bind<IWikiService>().To<WikiService>(); kernel.BindFilter<WikiFilter>(FilterScope.Global, 0); } } } 

and Global.asax remains unchanged. By the way, the default installation when installing the NinGet.MVC3 NuGet package.

+7
source

Source: https://habr.com/ru/post/886699/


All Articles