在我的索引頁,我使用生成URL以下Asp.net MVC 4網址資源無法找到
@foreach(var c in ViewBag.cities)
{
<li>@Html.ActionLink((string)c.CityName,"somepage",new{city=c.CityName, id=c.Id})</li>
}
將在以下格式
localhost:55055/1/city1
localhost:55055/2/city2
...
其中1生成的URL爲每個城市,2是Id和city1,城2是CITYNAME
我已經配置爲
routes.MapRoute(
name: "CityRoute",
url: "{id}/{city}",
defaults: new { controller = "Home", action = "somepage", id = UrlParameter.Optional, city = UrlParameter.Optional }
);
01的路線
在家庭控制器的somepage
操作方法:
public string somepage(int id, string city)
{
return city + " " + id;
}
即使我想這
public string somepage()
{
return "Hello";
}
,但它會導致Resource cannot be found
我試圖把斷點sompage
這是從來沒有擊中。
任何suggesstions
更新
正如評論所指出的@KD下面我改變了路線秩序和上面擺放的所有規則提到的原則之上。我也改變了方法
public ActionResult somepage(int id=0, string city="")
{
return Content(city + " " + id);
}
現在改變了行爲和默認規則
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
不用於正常的索引頁。相反,由於方法somepage
中的斷點被擊中,並且如果我在瀏覽器中放置了http://localhost:55055/2/cityname
,該頁面確實顯示了id和城市名。但現在默認路由不用於申請家庭http://localhost:55055/
請更新您的標題與您的具體問題.. – 2013-03-12 08:43:42
請確保您添加正確的順序路線。您添加的路線應位於默認路線的頂部。 – 2013-03-12 08:45:30
也試試這個動作方法public ActionResult somepage(string id,string city) – 2013-03-12 08:46:35