2012-12-25 116 views
0

我工作的一個圖像瀏覽器,所以我控制器命名爲Viewer,當我通過它routeValues它在URL中傳遞這樣的: http://www.mywebsite.com/Viewer?category=1&image=2URL重寫我的ASP.NET MVC網站

這裏是用來在該網頁上的鏈接:

@Url.Action("Index", new { category = p.Category, image = p.Image }) 

但我確實喜歡這個網址: http://www.mywebsite.com/Viewer/1/2

我曾嘗試在01做了一些小技巧RouteConfig類的方法,但我無法得到以前的結果。

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

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

     routes.MapRoute(
      name: "Viewer", 
      url: "{controller}/{action}/{category}-{image}", 
      defaults: new { controller = "Viewer", action = "Index", category = 1, image = 1 } 
     ); 
    } 
} 

有沒有人知道我可以在哪裏做到這一點?

非常感謝!

回答

3

你需要把你的更具體的路由默認路由之前,因爲路由在你宣佈他們同樣的順序進行評估:

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

     routes.MapRoute(
      name: "Viewer", 
      url: "{controller}/{action}/{category}/{image}", 
      defaults: new { controller = "Viewer", action = "Index", category = 1, image = 1 } 
     ); 

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