Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6017563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:08:47+00:00 2026-05-23T03:08:47+00:00

I am using asp.net mvc 3, ninject 2.0 and the ninject mvc 3 plugin.

  • 0

I am using asp.net mvc 3, ninject 2.0 and the ninject mvc 3 plugin.

I am wondering how do I get service layers into my filter(in this case an authorization filter?).

I like to do constructor inject so is this possible or do I have to property inject?

Thanks

Edit

I have this for property inject but my property is always null

  [Inject]
        public IAccountService AccountServiceHelper { get; set; }


        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            // check if context is set
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }

            // check if user is authenticated
            if (httpContext.User.Identity.IsAuthenticated == true)
            {
                // stuff here
                return true;
            }

            return false;
        }



    /// <summary>
    /// Application_Start
    /// </summary>
    protected void Application_Start()
    {

        // Hook our DI stuff when application starts
        IKernel kernel = SetupDependencyInjection();

        RegisterMaps.Register();

        AreaRegistration.RegisterAllAreas();

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

    }


    public IKernel SetupDependencyInjection()
    {
        IKernel kernel = CreateKernel();
        // Tell ASP.NET MVC 3 to use our Ninject DI Container
        DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

        return kernel;
    }

    protected IKernel CreateKernel()
    {
        var modules = new INinjectModule[]
                          {
                             new NhibernateModule(),
                             new ServiceModule(),
                             new RepoModule()
                          };

        return new StandardKernel(modules);
    }


public class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind<IAccountService>().To<AccountService>();
    }

}

Edit

I upgraded to ninject 2.2 and get finally got it work.

Edit 2

I am going to try and do the constructor way for my authorize filter but I am unsure how to pass in the Roles. I am guessing I have to do it through ninject?

Edit 3

This is what I have so far

 public class MyAuthorizeAttribute : AuthorizeAttribute 
    {
        private readonly IAccountService accountService;

        public MyAuthorizeAttribute(IAccountService accountService)
        {
            this.accountService = accountService;
        }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            return base.AuthorizeCore(httpContext);
        }
    }

  this.BindFilter<MyAuthorizeAttribute>(FilterScope.Controller, 0)
                .WhenControllerHas<MyAuthorizeAttribute>();

  [MyAuthorize]
    public class MyController : BaseController
    {
}

It tells me it want’s a no parameter constructor. So I must be missing something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:08:47+00:00Added an answer on May 23, 2026 at 3:08 am

    The problem with filters is that they are attributes. And if you define a constructor of an attribute that expects some dependency you will never gonna be able to apply it to any method: because all values that you pass to attributes must be known at compile time.

    So basically you have two possibilities:

    1. Use Ninject to apply the filter globally instead of decorating your controllers/actions with it:

      public interface IFoo { }
      public class Foo : IFoo { }
      
      public class MyFooFilter : AuthorizeAttribute
      {
          public MyFooFilter(IFoo foo)
          {
      
          }
      }
      

      and then configure the kernel:

      kernel.Bind<IFoo>().To<Foo>();
      kernel.BindFilter<MyFooFilter>(FilterScope.Action, 0).When(
          (controllerContext, actionDescriptor) => 
              string.Equals(
                  controllerContext.RouteData.GetRequiredString("controller"),
                  "home",
                  StringComparison.OrdinalIgnoreCase
              )
      );
      
    2. Use property injection:

      public interface IFoo { }
      public class Foo : IFoo { }
      
      public class MyFooFilter : AuthorizeAttribute
      {
          [Inject]
          public IFoo Foo { get; set; }
      }
      

      and then configure the kernel:

      kernel.Bind<IFoo>().To<Foo>();
      

      and decorate some controller/action with your custom filter:

      [MyFooFilter]
      public ActionResult Index()
      {
          return View();
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble injecting AutoMapper into an ASP.NET MVC 2 application using Ninject.
I'm using the Ninject.Web.Mvc (the MVC 2 version) add-on with ASP.NET MVC 2. This
Using asp.net MVC I'd like to do this inside a view: <%= Html.TextBox(textbox1, null,
I know this site is written using ASP.Net MVC and I do not see
We are using ASP.net MVC. Which of these is the best DI framework Ninject
I'm using Ninject 2 with an ASP.NET MVC web app. All the dependencies are
I was trying Ninject in a Asp.net Mvc application and I was wondering what
In my asp.net mvc application I'm using Ninject as a DI framework. My HttpAccountService
I'm using ASP.NET MVC 2 to implement a web service and I have a
I've got a simple web application using ASP.NET MVC3 and Ninject.Web.MVC (the MVC3 version).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.