也許我不明白asp mvc路由的真正目的。
我創建了一個應用程序,現在我需要修復我的網址a以便更容易理解。
例如,我有控制器Home
和動作Index
的區域Cities
。
所以在這裏我需要像這樣的網址:localhost/London
但目前的路由我得到localhost/cityPage/Home
。
我的問題是我可以以某種方式傳遞像city name
這樣的參數並使URL像我想要的那樣?
這是我的Global.asax當前默認路由如何控制路由值
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "CityPage", controller = "Home", action = "Index", id = "" },
new string[] { "MyProject.Areas.Cities.Controllers" }).DataTokens.Add("area", "Cities");
新的路由:
routes.MapRoute(null,
"CityPage/{cityName}",
new
{
area = "CityPage",
controller = "Home",
action = "Index"
}
);
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "CityPage", controller = "Home", action = "Index", id = "" },
new string[] { "MyProject.WebUI.Areas.CityPage.Controllers" }).DataTokens.Add("area", "CityPage");
鏈接的例子,我點擊
@Html.ActionLink("City London", "Index", "Home", new { cityName = "London" }, null)
我加入這個默認之前,但URL不瓚ged :(我如何控制{id}參數? – 1110 2012-01-08 15:02:07
請將您想要控制的{id}參數添加到您的問題中。 – danludwig 2012-01-08 15:12:53
我添加了我的家庭/索引操作方法代碼,也許我犯了一些錯誤。 – 1110 2012-01-08 15:17:35