2009-08-20 177 views
2

你好我想要創建一個類似如下的URL:asp.net MVC創建我自己的路由

黑色/花崗岩/檯面

其中黑色和花崗岩會改變,所以我試圖創建我自己在的global.asax.cs路線,像這樣:

routes.MapRoute("Kitchen", "kitchen/[color]/[surface]/[type]", 
         new {controller = "kitchen", action = "surface"}); 

網址更改爲廚房/黑色/花崗岩/檯面

這樣,我想我可以創建一個名爲廚房用的控制器 我這個代碼看起來ction稱爲表面像這樣:

public ActionResult surface(string color, string surface, string type) 
    { 
     ViewData["color"] = color; 
     ViewData["surface"] = surface; 
     ViewData["type"] = type; 
     return View(); 
    } 

但是我似乎無法得到它的工作,我得到這個URL錯誤404,儘管我自定義映射,任何人都可以點我的方向讀書,我一直在這裏閱讀此頁:http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

這是給我的想法,因爲他有查詢和網頁的代碼是最新的小油井管,因爲我使用MVC預覽2個

千恩萬謝

回答

1

現在的工作方式,是在你的Global.asax的,你會想是這樣的:

routes.MapRoute("Kitchen Surface Route", 
       "kitchen/{color}/{surface}/{type}", 
       new {controller = "kitchen", action = "surface", color="", surface = "", type=""}); 

然後你就必須像這樣一個ActionLink的:

<%= Html.ActionLink("Link Text", "Kitchen", "surface", new {color="theColor", type="theType", surface="surfaceType"}, null) %> 

它可以得到有時候會有些複雜的路線。您也可以使用Phil Haack's Route Debugger來幫助您。