Silent
Silent

Reputation: 549

asp.net mvc areas and main site w/ same controller names

I am to the point I have created my main front end site. I have added my areas and starting work on the backend. Both sections are going well except one thing thats bugging me.

I want to have a Home controller in both areas but get the error that I cannot have more then one. I have followed a few tutorials with no luck. Here is the last one I tried.

I have started a new project just for testing this here is my Register Routes code:

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

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

I have put this code in my Global.asax.cs file? That doesn't seem to help any. Here is my actual error:

The request for 'Home' has found the following matching controllers: Test.Areas.Portal.Controllers.HomeController Test.Controllers.HomeController

Any tips or pointers would be great! I have gone over the other questions asked here but they all say to do what I'm already doing which is to add the function I did above.


Path to controllers:
Areas - Portal - Controllers - HomeController.cs
Controllers - HomeController.cs

Upvotes: 3

Views: 1737

Answers (2)

Patrick McDonald
Patrick McDonald

Reputation: 65441

Try inserting the following immediately after the IgnoreRoute line

AreaRegistration.RegisterAllAreas();

EDIT:

I tried creating a new project and adding a new Area with a Home controller. Just running the application I was able to reproduce your error.

Making the same code change as you did fixed this problem.

I would suggest right-clicking on the ASP.NET Development server icon in the system tray and clicking Exit, then try rerunning the application.

If this does not fix it, double check that the namespace you specify in your RegisterRoutes method exactly matches the full namespace of the default controllers.

Upvotes: 4

gmail user
gmail user

Reputation: 2783

I think you need one more MapRoute above the default one.

routes.MapRoute(.....,"Area/Portal/{controller}/{action}/{id},.....,new string[]{...namespce....});

or may be looked at this post http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx

Upvotes: 0

Related Questions