Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASPASP.NETMVC
I'm newbie in asp mvc, currently, my demo project structure like this:
 
Areas -- Comment -- Controller -- HomeController
                               -- ManageController
Controller -- HomeController
          |-- CommentController
                     |____ PostMsg
                     |____ DeleteMsg
Views -- Home
     |     |--- Index.cshtml
     |-- Comment
           |--- PostMsg.cshtml
           |--- DeleteMsg.cshtml
When I browsing url like :
    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 Frown | :-(
here is my global.asax.cs route config:
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:
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 Smile | :)
 
ISSUE RESOLVED, edit area registration route config (work around):
 
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 7-Nov-12 4:27am
Van Hua893
Edited 7-Nov-12 5:33am


3 solutions

Edit area registration route config (work around):
 
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" }
            );
        }
  Permalink  
this is my example code thats work properly
     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);  
        
        }
  Permalink  
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:
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" }
                );
  Permalink  
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 6,824
1 Prasad_Kulkarni 3,671
2 _Amy 3,312
3 OriginalGriff 3,309
4 CPallini 2,925


Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid