user295541
user295541

Reputation: 1015

ASP.NET MVC Custom Routing

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.

Upvotes: 0

Views: 951

Answers (3)

scazy
scazy

Reputation: 1

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 }
  );
}

Upvotes: 0

Tee Wu
Tee Wu

Reputation: 579

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
   );

Upvotes: 2

Matt Lacey
Matt Lacey

Reputation: 65586

Your routes are essentially the same!

How are getting the URI with the query string?

Upvotes: 1

Related Questions