1

我一定做错了什么,因为我已经复制了很多关于这个主题的答案。我的属性绑定没有被击中,我不知道为什么。

控制器.cs

[NatGeoUserAccessAuthorization]
        [HttpGet]
        public virtual ActionResult Teacher(string id)
        {

属性/过滤器

public class NatGeoUserAccessAuthorizationAttribute : FilterAttribute{}
    public class NatGeoUserAccessAuthorizationFilter : IAuthorizationFilter 
    {
        private readonly IUsersService _usersService;

        public NatGeoUserAccessAuthorizationFilter(IUsersService usersService)
        {
            _usersService = usersService;
        }

        public string QueryStringName { get; set; }

        #region Implementation of IAuthorizationFilter

        public void OnAuthorization(AuthorizationContext filterContext)
        {
            if (!_usersService.HasUserAccess(filterContext.HttpContext.User.Identity.Name, filterContext.HttpContext.Request.QueryString[QueryStringName ?? "id"]))
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "action", "AccessDenied" }, { "controller", "Error" } });
            }
        }

        #endregion
    }

全球.asax

protected void Application_Start()
        {            
            // NLog Custom Layouts
            ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("utc_date", typeof(UtcDateRenderer));
            ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("web_variables", typeof(WebVariablesRenderer));

            // Setup IoC Container
            DependencyResolver.SetResolver(Bootstrap.Configure((kernel) =>
            {
                kernel.Bind<IDatabaseFactory>().To<DatabaseFactory<MySqlConnection>>().InRequestScope().WithConstructorArgument("connectionString", Config.Data.MySQLConnection);

                ManagerBindings.Register(kernel);
                ProviderBindings.Register(kernel);
                RepositoryBindings.Register(kernel);
                ServiceBindings.Register(kernel);
                ValidationBindings.Register(kernel);

                kernel.BindFilter<NatGeoUserAccessAuthorizationFilter>(FilterScope.Action, 0).WhenActionMethodHas<NatGeoUserAccessAuthorizationAttribute>();

                kernel.Bind<IUserProfile>().To<UserProfile>();
                kernel.Inject(Roles.Provider);
            }));

            // Custom Default Model Binder
            ModelBinders.Binders.DefaultBinder = new ValidationModelBinder();

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

引导程序

public class Bootstrap
    {
        public delegate void IocRegistrationDelegate(IKernel kernel);

        public static IDependencyResolver Configure(IocRegistrationDelegate serviceBindings)
        {
            var kernel = new StandardKernel();

            serviceBindings.Invoke(kernel);

            return new NinjectDependencyResolver(kernel);
        }
    }
4

1 回答 1

2

您正在使用自己的不支持过滤器绑定的引导机制。在http://github.com/ninject/ninject.web.mvc/wiki上阅读文档如何使用 Ninject.Mvc3 附带的文档

于 2012-04-18T17:57:36.950 回答