考慮使用如此Nov. 2008 Phil Haack blog post中所述的區域約定的ASP.NET MVC 1.0項目。這個解決方案一旦建立就很好用!ASP.NET MVC:一個參數未命名爲ID和非整數的操作方法
由於我對ASP.NET MVC的路由規則知之甚少,所以我的麻煩就開始了。
我的目的是創建一個操作方法和URL的結構是這樣的:
http://mysite/Animals/Dogs/ViewDog/Buster
DogsController.ViewDog()
看起來是這樣的:
public ActionResult ViewDog(string dogName)
{
if (dogName!= null)
{
var someDog = new DogFormViewModel(dogName); //snip a bunch more
return View(someDog);
}
else { return View("DogNotFound"); }
}
眼下的任務是確保RegisterRoutes()
有正確的條目。
UPDATE
這裏的新路徑映射:
routes.MapRoute("ViewDog", "Animals/{controller}/{action}/{dogName}",
new { controller = "Dogs",
action = "ViewDog", dogName = "" });
創建到URL鏈接:按預期的方式創建
<%= Html.RouteLink("Brown Buster", "ViewDog", new RouteValueDictionary(new { controller="Dogs", action="ViewDog", dogName="Buster" }))%>
的URL。感謝Craig Stuntz and his blog post on Html.RouteLink
。
http://mySite/Animals/Dogs/ViewDog/Buster
新的問題:帕拉姆dogName
不皮卡從URL字符串值 「剋星」。對該方法的調用成功,但參數的計算結果爲null。
問題
你怎麼能:
- 化妝用細繩這條路線的工作,並刪除路由的默認慣例
int id
? 我想從int
改變參數的名稱。
默認值是id,而不是int id。路由令牌是無類型的。 – 2009-09-24 00:26:36
你的路線在什麼順序中定義?此路由是否在默認路由中帶有'id'? – 2010-02-16 17:28:15