2010-12-13 4 views
0

Я хотел бы создать пользовательскую маршрутизацию в моем приложении.ASP.NET MVC Custom Routing

Я добавил новый маршрут в глобальном файле asax:

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

Он отлично работает, когда я использую UserProfileController:

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

Но маршрутизация по умолчанию не работает!

Я вижу это http://localhost:7738/Blog/Info?id=2 вместо этого http://localhost:7738/Blog/Info/2

Кто-нибудь может мне помочь?

Thanks l.

+0

локального хоста? шутки в сторону? – bevacqua

+0

@ Нико вы пробовали эти ссылки? шутки в сторону? – dotjoe

ответ

2

Может быть, вы фиксированной маршрут к:

routes.MapRoute(
     "Profile",           // Route name 
     "UserProfile/{action}/{userName}",       // URL with parameters 
     new { action = "Index", userName = UrlParameter.Optional } // Parameter defaults 
    ); 
1

Ваши маршруты по существу одинаковы!

Как получить URI с помощью строки запроса?

+0

+1, точно, '{controller}/{action}/{userName}' и '{controller}/{action}/{id}' функционально идентичны. –

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