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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:11:55+00:00 2026-05-28T14:11:55+00:00

I have a controller factory which depending on the route values builds a controller

  • 0

I have a controller factory which depending on the route values builds a controller in different ways. These are:

  1. If the route values match with a “module” route it determines the correct type using the route values to calculate the qualified type name. This is, if user types [http://localhost:8080/siijyp/modules/Personas/Naturales/Documento/Display?id=1], the controller factory builds and returns an OIMSIIJYP.Web.*Personas.Naturales.Documento*Controller
  2. If the route values match with a “module” route and it also contains a “script” value, the controller factory builds the controller in runtime using Mono.CSharp
  3. If the route values don´t match with a module, it builds the controller as the DefaultControllerFactory does it.

It is rather evident all these if statements are wrong, I also know there is a IControllerActivator type which is used for the DefaultControllerFactory to create a controller given a controller type. But what I don´t know is which is the best way to implement these controller creation strategies in MVC 3.

Any idea?

The code:

public IController CreateController(RequestContext requestContext, 
    string controllerName)
{
    #region Argument checking
    if (requestContext == null)
{
        throw new ArgumentNullException("requestContext");
    }
    if (string.IsNullOrEmpty(controllerName))
    {
        throw new ArgumentException(
            "ControllerName cannot be null nor empty",     
            "controllerName");
    }
    #endregion
    Type controllerType = null;

    var routeData = requestContext.RouteData;
    if (IsRequestingAModule(routeData))
    {
        var module = routeData.Values["module"];
        var submodule = routeData.Values["submodule"];

        var controllerQualifiedName = string.Format(
            "OIMSIIJYP.Web.{0}.{1}.{2}Controller", module, 
            submodule, controllerName);
        try
        {
            controllerType = Type.GetType(controllerQualifiedName, true, 
                true);
            if (IsRequestingAScriptingBehavior(routeData))
            {
                return GetControllerRuntimeUsingMono(routeData, 
                    controllerName, controllerType);
            }
            else
            {
                return _container.Resolve(controllerType) as IController;
            }
        }
        catch (TypeLoadException)
        {
            throw new HttpException(404, 
                string.Format(CultureInfo.CurrentCulture, 
                "Controller not fount", new object[] {
                    requestContext.HttpContext.Request.Path
                }));
        }
    }
    else
    {
        var controllerTypes = 
            from type in Assembly.GetExecutingAssembly().GetTypes()
            where StringComparer.CurrentCultureIgnoreCase.Compare(
                type.Name, controllerName + "Controller") == 0
            select type;

        switch (controllerTypes.Count())
        {
            case 0:
                throw new HttpException(404, 
                    string.Format(CultureInfo.CurrentCulture, 
                    "Controller not found", new object[] {
                        requestContext.HttpContext.Request.Path
                    }));
            case 1:
                controllerType = controllerTypes.First();
                break;
            default:
                throw UnityControllerFactory.
                CreateAmbiguousControllerException(routeData.Route, 
                    controllerName, controllerTypes.ToList());
        }
    }

    return _container.Resolve(controllerType) as IController;
}
  • 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-28T14:11:56+00:00Added an answer on May 28, 2026 at 2:11 pm

    I think it has another workaround, you can use Area. Suppose I have two controller same name but stored in two different place one of them inside of area and other is normal, For example

    admin/Controller/CommonController.cs //here admin is an area name
    Controller/CommonController.cs
    

    Now i’ve two Action both are try to render two different view.

    @Html.Action("SystemInfo", "Common", new { area = "Admin" }) // here Admin is the area name
    @Html.Action("HeaderLinks", "Common")
    

    the same facility can be obtain using routing.

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

Sidebar

Related Questions

I have a controller action which I would like to call another controller action.
I have a controller called articles , which creates the articles model which gets
I have controller which extends Controller_Template , and also i have quite few functions
First, I have a dbcontext factory which is defined public class DatabaseFactory : Disposable,
I have a controller which is supposed to create version dependend instances (currently not
Currently I have Windsor controller factory that is working well. However, I am changing
I have a controller with the following annotation @Autowired ServletContext servletContext; which seems to
in asp.net-mvc I have the Windsor Controller Factory that injects all the dependencies into
I have controller with action new , and I want it to create ActiveRecord::Base
When submitting one form from view, how can i also read or have controller

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.