Controller with same name as an area - Asp.Net MVC4
Asked Answered
A

2

7

I have a Contacts controller in the main/top area, and I have an area named "Contacts".

I get POST 404s to the Contacts controller if I register my areas before I register my top-level routes:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        ModelBinders.Binders.DefaultBinder = new NullStringBinder();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

And, if I register my areas after my routes, my 404s to the Contacts controller goes away, but my routes to the Contacts area are now 404s.

...lots of duplicate controller name questions logged, but I haven't found a specific scenario where the area is the same name as the controller.

...probably an easy fix. Would appreciate help. :-D

fwiw, I am registering my Contacts area with an explicit namespace:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "MyMvcApplication.Controllers" }
        );
    }
Araujo answered 4/11, 2013 at 21:50 Comment(1)
Are you specifying the area when using Html.Action, etc.?Paphlagonia
T
24

There are two things to consider

  1. In Application_Start() method register areas first AreaRegistration.RegisterAllAreas();.

  2. In case of conflicting name, use the namespaces in RouteConfig.cs file of App_Start folder as well as all the routes defined in routes (like ContactsAreaRegistration.cs)

To replicate your scenario, I created a sample application and able to access successfully both URLs given below:

http://localhost:1200/Contacts/Index

http://localhost:1200/Contacts/contacts/Index

The structure of my application looks like:

enter image description here

Here inside ContactsAreaRegistration.cs file we are having following code:

public class ContactsAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Contacts";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Contacts_default",
                "Contacts/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "MvcApplication1.Areas.Contacts.Controllers" }
            );
        }
    }

Hope it will help you. If you need I can send sample application code which I have created. Thanks.

Thursday answered 15/11, 2013 at 19:14 Comment(2)
Overloading the MapRoute method with namespace solves the issue.Guan
It's very strange, because it didn't solve my problem. When I go to /Contacts/Index, then it says that page is not found. I added namespaces. Application thinks that I try to go to /Contacts/Index/Index. Only solution which I found is removing Action = "Index" from defaults argument of MapRoute method or using /Contacts URL instead of /Contacts/Index.Polad
W
0

For MVC5, I did what @Snesh did but that didn't fully work. It would only resolve the controllers in my area but not in the root of the project if they had the same name. I wound up having to specify the namespaces as parameters in both the RegisterArea method and the RegisterRoutes method in my RouteConfig.cs.

RouteConfig.cs

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            // This resolves to the Controllers folder at the root of the web project
            namespaces: new [] { typeof(Controllers.HomeController).Namespace }
        );
    }

AreaRegistration.cs

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Handheld_default",
            "Handheld/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { typeof(Areas.Handheld.Controllers.HomeController).Namespace }
        );
    }
Woollen answered 13/8, 2018 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.