Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP ASP.NET MVC
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
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
Van Hua at 7-Nov-12 10:58am
   
AreaRegistration.RegisterAllAreas(); already did that for me
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 8 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100