Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to add my routing pattern along with the defult routing rules.

C#
routes.MapRoute(
                name: "myRouting",
                url: "{action}/{controller}/{id}",
                defaults: new {action = "Index",  controller = "Home", id = UrlParameter.Optional }
            );



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

            routes.MapRoute(
                name: "myRouting",
                url: "{action}/{controller}/{id}",
                defaults: new {action = "Index",  controller = "Home", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

With these routing,
VB
http://localhost:29535/Edit/Book   ---> Working
http://localhost:29535/Book/Edit   ---> Error (The resource cannot be found)
Here, Book is Controller, Edit is Action


But I was expecting both should work.

What I'm basically missing to understand here. Please help me understand the rules behind this.

Note: If I make changes in my url (in myRouting) as
C#
url: "{action}/2/{controller}/{id}",


VB
http://localhost:29535/Edit/2/Book ---> Working
http://localhost:29535/Book/Edit   ---> Working


Thanks,
Prabhu
Posted
Updated 14-Nov-14 3:13am
v2

1 solution

The issue is that your route signatures are precisely the same, and there is no way for your Route table to differentiate between the two. The first one evaluated in this instance will be the route that is used.

The easiest trick here is to modify the signature of your new route:

C#
routes.MapRoute(
                name: "myRouting",
                url: "myroute/{action}/{controller}/{id}",
                defaults: new {action = "Index",  controller = "Home", id = UrlParameter.Optional }
            );


And append "myroute/" to your links that use that pattern.
 
Share this answer
 
Comments
@Kathukkutti 14-Nov-14 9:25am    
@Nathan Minier, Thanks for your comment. I've already tried that and was working fine. But I'm trying to understand why "{action}/{controller}/{id}" is not working. Thanks.
Nathan Minier 14-Nov-14 9:37am    
As I noted in my first sentence, the signatures are precisely the same.

Let's look at your example:
http://localhost:29535/Edit/Book ---> Working
http://localhost:29535/Book/Edit ---> Error (The resource cannot be found)
Here, Book is Controller, Edit is Action

For your error item, the route handler is looking for controller Edit with action Book, because that route is exactly the same as the default route and there is no way for it to tell the 2 apart. The route handler will take the first matching pattern, and you have 2 patterns that that string/string/string. By adding a new part to the signature, in this case a static string, you change the signature and what will match against it.

For more context, look at:
http://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC

EDIT: I realize that you may assume that if 1 route does not match to a controller, it will try another. That's not how the route handler works. It will pick exactly one route that best matches the given signature, and will use that route for the processing of the request. It will not failover to another route out of the box; it would take some surgery on the route handler.
@Kathukkutti 17-Nov-14 1:09am    
Thanks a lot. Clear message.
Dave Kreskowiak 14-Nov-14 9:40am    
He already told you why it's not working.

Both of your signatures are {string}/{string}/{integer}. Since the first route matched always wins, the second one is never evaluated.

You cannot do what you want in your original post. You cannot have the controller name and actions switched an expect either format to work. It MUST be one way and used consistently throughout your web app.

To pull off what you want to do, you'd have to write your own implementation of the MvcRouteHandler.
@Kathukkutti 17-Nov-14 1:10am    
Thank you. Now I'm clear.

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