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

  • SEARCH
  • Home
  • 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 7921111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:27:53+00:00 2026-06-03T16:27:53+00:00

In my MVC 2 project, I originally used Ninject 2 and wrote this version

  • 0

In my MVC 2 project, I originally used Ninject 2 and wrote this version of the NinjectControllerFactory:

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel kernel = new StandardKernel(new HandiGamerServices());

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        try
        {
            if (controllerType == null)
            {
                return base.GetControllerInstance(requestContext, controllerType);
                // return null;
            }
        }
        catch (HttpException ex)
        {
            if (ex.GetHttpCode() == 404)
            {
                IController errorController = kernel.Get<ErrorController>();
                ((ErrorController)errorController).InvokeHttp404(requestContext.HttpContext);

                return errorController;
            }
            else
            {
                throw ex;
            }
        }

        return (IController)kernel.Get(controllerType);
    }

Of most importance is the retrieval of my ErrorController, which allows me to gracefully handle a multitude of HTTP errors.

The problem is that I upgraded to the MVC 2 extension via Nuget, so a NinjectControllerFactory is already provided. Would it be possible to use my own override of GetControllerInstance? If so, how?

  • 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-06-03T16:27:56+00:00Added an answer on June 3, 2026 at 4:27 pm

    I do exactly this, and for precisely the same reason. In Global.asax.cs, I add this to my OnApplicationStarted override (declared virtual in NinjectHttpApplication):

    ControllerBuilder.Current.SetControllerFactory(
        new MyControllerFactory(ControllerBuilder.Current.GetControllerFactory()));
    

    This means you’re creating your own controller factory, but providing it with the default implementation to do the heavy lifting.

    Then define your controller factory like so:

    public class MyControllerFactory : IControllerFactory
    {
        private IControllerFactory defaultFactory;
    
        public MyControllerFactory(IControllerFactory defaultFactory)
        {
            this.defaultFactory = defaultFactory;
        }
    
        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            try
            {
                var controller = defaultFactory.CreateController(requestContext, controllerName);
                return controller;
            }
            catch (HttpException e)
            {
                // Pasted in your exception handling code here:
                if (ex.GetHttpCode() == 404)
                {
                    IController errorController = kernel.Get<ErrorController>();
                    ((ErrorController)errorController).InvokeHttp404(requestContext.HttpContext);
    
                    return errorController;
                }
                else
                {
                    throw ex;
                }
            }
        }
    
        public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
        {
            return defaultFactory.GetControllerSessionBehavior(requestContext, controllerName);
        }
    
        public void ReleaseController(IController controller)
        {
            defaultFactory.ReleaseController(controller);
        }
    }
    

    As you can see, we’re just using the default (Ninject) controller factory for most purposes unless it can’t find the page. For obtaining the error controller, you can either pass in the kernel as you were already doing, or just call defaultFactory.CreateController using the error controller name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Class Library project and MVC project in the one solution. My
I am working with an ASP.NET MVC project which was originally started from the
My MVC project uses the default location (/Content/...) So where this code: <div id=headerstyle=background-image:
This is for .net MVC project i'm working on I have the following classes:
This is the weirdest error I have ever encountered. This MVC web project was
I am using this sample Spring MVC project with AJAX / JSON support: https://src.springframework.org/svn/spring-samples/mvc-ajax/
I've used EF 4.1 ( Code First ) in an MVC 3 project a
My ASP.NET MVC project has some chapter class and related tag class They have
In our ASP.NET MVC project we have a Strings.resx file and the accompanying autogenerated
In my MVC project I have a chart being generated on my view as

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.