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 1958244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T08:17:06+00:00 2026-05-17T08:17:06+00:00

I have an ASP.NET MVC 2 application with a custom StructureMap controller factory to

  • 0

I have an ASP.NET MVC 2 application with a custom StructureMap controller factory to handle dependency injection for my controllers:

public class StructureMapControllerFactory : DefaultControllerFactory
{
    public override IController CreateController(RequestContext context, string controllerName)
    {
        Type controllerType = base.GetControllerType(context, controllerName);
        return ObjectFactory.GetInstance(controllerType) as IController;
    }
}

I would like to know how I can handle exceptions in this controller factory so that they can be redirected to the ~/Views/Shared/Error.aspx in the same way as they are in a controller that has the HandleError attribute. Currently exceptions don’t do this despite having the CustomErrors attribute set to “On”.

At the moment I can generate such an exception using a URL like “~/DoesNotExist/edit/1”. With the default route:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

MVC matches this route and passes controller name “DoesNotExist” to my controller factory. The GetControllerType then returns null and causes a null reference exception in the call to StructureMap. I would then like to be able to handle this exception.

Note that adding a subsequent catch all route will not resolve this problem – MVC matches the default route.

I know I could solve this particular problem by putting constraints on the default route for controller but the question is more general about how I can use the normal MVC ~/Views/Shared/Error.aspx in the factory.

Note that I don’t want the answer to require tight coupling of the controller factory to the particular MVC application. Ideally this factory should be in a referenced assembly not in the same solution.

  • 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-17T08:17:06+00:00Added an answer on May 17, 2026 at 8:17 am

    Maybe you can adapt this controller factory to handle the exceptions.

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

Sidebar

Related Questions

In an ASP.NET MVC application when using a custom Dependency Injection container (in this
I have an ASP.NET MVC application using Authorization Attributes on Controllers and Actions. This
I have an ASP.NET MVC web application that implements a custom membership provider. The
I have an ASP.NET MVC 2 application in which I am creating a custom
I have implemented custom errors in my asp.net mvc application by following this article
I'm writing my first asp.net mvc application and I have a question about custom
We have an ASP.NET MVC application for which we have developed our own custom
I have a asp.net mvc application and am trying to get custom errors working
I am developing an asp.net mvc application and have created my custom user database
I have one asp.net mvc 3 application (www.name.com) with custom module added to web.config:

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.