ASP.NET MVC 3和全局筛选注射 [英] ASP.NET MVC 3 and Global Filter Injection

查看:128
本文介绍了ASP.NET MVC 3和全局筛选注射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好是一个试图执行注射全局筛选器。
该过滤器看起来是这样的。

Hello am an trying to implement a global filter with injection. The filter looks like this.

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

和我有我的Global.asax附注射过滤器的方法如下:

And i have attached the filter with injection the following way 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 is the filter never fired when the application runs, have i not implemented it correctly?

推荐答案

我使用〜/ App_Start / NinjectMVC3.cs 文件来配置Ninject内核会建议你:

I would recommend you using the ~/App_Start/NinjectMVC3.cs file 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);
        }
    }
}

和在Global.asax保持不变。通过当您安装Ninject.MVC3的NuGet包是默认安装的方式。

and the Global.asax stays unchanged. By the way that's the default setup when you install the Ninject.MVC3 NuGet package.

这篇关于ASP.NET MVC 3和全局筛选注射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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