2009-04-10 46 views
2

在我的MVC應用程序,爲什麼爲什麼ASP.NET MVC路徑顯示變量?

Return RedirectToAction("Edit", "Forms", New With {.member = "123"}) 

回報

http://localhost:13/Forms/Edit?member=123 

http://localhost:13/Forms/Edit/123 

insted的?

又爲何

<%=Html.ActionLink("MyLink", "Edit", "Forms", New With {.member = "123"}, Nothing)%> 

做同樣的事情?

+0

郵政MVC路由表,你會因爲這個方法在Global.asax中的得到更好的答案... – 2009-04-10 20:09:01

回答

5

標準路由設置爲使用id作爲第三個參數。將「member」更改爲「id」,您將獲得您期望的路線。

Return RedirectToAction("Edit", "Forms", New With { .id = "123"}) 
6

正如tvanfosson所說,「id」是默認路由引擎設置要查找的內容。還有其他的第三個參數,它將作爲查詢字符串進行計算。

爲什麼?

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

您可以通過添加額外的routes.MapRoute()線改變這一點,就像這樣:

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

routes.MapRoute(
    "Default2", 
    "{controller}/{action}/{member}", 
    new { controller = "Home", action = "Index", member = "" } 
);