Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm newbie in asp mvc, currently, my demo project structure like this:

C#
Areas -- Comment -- Controller -- HomeController
                               -- ManageController

C#
Controller -- HomeController
          |-- CommentController
                     |____ PostMsg
                     |____ DeleteMsg
Views -- Home
     |     |--- Index.cshtml
     |-- Comment
           |--- PostMsg.cshtml
           |--- DeleteMsg.cshtml

When I browsing url like :
C#
http://localhost/Comment/Manage/ --> return view successfully
http://localhost/Comment/PostMsg --> error "The resource cannot be found."

Anyone have any idea why asp mvc doesn't resolve my controller :-(
here is my global.asax.cs route config:
C#
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                    namespaces: new[] { "Demo.Web.Controllers" }
                );

here is my area registration route config:
C#
public override void RegisterArea(AreaRegistrationContext context)
            {
                context.MapRoute(
                    "Comment_default",
                    "Comment/{controller}/{action}/{id}",
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                    new[] { "Demo.Web.Areas.Comment.Controllers" }
                );
            }

Problem : Comment/PostMsg url was resolved as an Controller in Comment Area

Goal : Comment/PostMsg url was resolved as an Action of CommentController
Any help would be appreciated :-)

ISSUE RESOLVED, edit area registration route config (work around):

C#
public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Comment_default",
                "Comment/PostMsg",
                new { controller = "Comment", action = "PostMsg", id = UrlParameter.Optional },
                new[] { "Demo.Web.Controllers" }
            );

            context.MapRoute(
                "Comment_default",
                "Comment/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new[] { "Demo.Web.Areas.Comment.Controllers" }
            );
        }
Posted
Updated 10-Jul-18 23:52pm
v6

Add the following Code in Global.asax file.
protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
           GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            
        }
 
Share this answer
 
Comments
CHill60 11-Jul-18 8:54am    
Did you not notice that this was resolved over 5 years ago? The green banner on the heading of a solution indicates that the OP has accepted the solution.
Stick to answering new posts where the OP still needs help
Go to the properties page of the Web project and select the Web tab. In the Start Action section, set it to Specific Page, but leave the textbox empty.
 
Share this answer
 
Edit area registration route config (work around):

C#
public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Comment_default",
                "Comment/PostMsg",
                new { controller = "Comment", action = "PostMsg", id = UrlParameter.Optional },
                new[] { "Demo.Web.Controllers" }
            );
 
            context.MapRoute(
                "Comment_default",
                "Comment/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new[] { "Demo.Web.Areas.Comment.Controllers" }
            );
        }
 
Share this answer
 
this is my example code thats work properly
C#
     public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
     "viewstudent", // Route name
     "view/viewstudent/{stdid}", // URL with parameters
     new { controller = "Movie", action = "SelectById", id = UrlParameter.Optional } // Parameter defaults
     );

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

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
         
        RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);  
        
        }
 
Share this answer
 
Comments
Van Hua 7-Nov-12 11:22am    
Hmm.. It look like you don't get my problem :-( The main problem here is the name conflict between CommentController with CommentAreas.
In your demo, you add specific url which have different name with your controller and don't create any conflict areas name.
ok
you can add directly two maprout in globalasx.cs
for example you can do this:
C#
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                    namespaces: new[] { "Demo.Web.Controllers" }
                );


routes.MapRoute(
                    "Comment_default",
                    "Comment/{controller}/{action}/{id}",
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                    new[] { "Demo.Web.Areas.Comment.Controllers" }
                );
 
Share this answer
 
v2
Comments
Van Hua 7-Nov-12 10:58am    
AreaRegistration.RegisterAllAreas(); already did that for me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900