0

I would like to create a custom routing in my app.

I added a new route in the Global asax file:

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

        routes.MapRoute(
           "Profile",                                           // Route name
           "{controller}/{action}/{userName}",                            // URL with parameters
           new { controller = "UserProfile", action = "Index", userName = 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
        );
    }

It works fine when I use the UserProfileController:

http://localhost:7738/UserProfile/Info/chopin

But the Default routing is not working!

I see this http://localhost:7738/Blog/Info?id=2 instead of this http://localhost:7738/Blog/Info/2

Anybody can help me?

Thanks l.

1
  • @Nico you tried those links? seriously? Commented Dec 13, 2010 at 17:22

3 Answers 3

2

Maybe you can fixed your route to:

 routes.MapRoute(
       "Profile",                                           // Route name
       "UserProfile/{action}/{userName}",                            // URL with parameters
       new { action = "Index", userName = UrlParameter.Optional }  // Parameter defaults
   );
Sign up to request clarification or add additional context in comments.

Comments

1

Your routes are essentially the same!

How are getting the URI with the query string?

1 Comment

+1, exactly, {controller}/{action}/{userName} and {controller}/{action}/{id} are functionally identical.
0
public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  routes.MapRoute(
    "UserProfile",
    "UserProfile/{action}/{userName}",
    new { contoller = "UserProfile", action = "Index", userName = UrlParameter.Optional }
  );

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.