我想配置自定義URL在RouteConfig.cs文件作爲默認的:當我寫的以下內容檢索查詢字符串,並迫使自定義網址到頁面
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
}
以上工作正常網址:1路由
http://localhost:1234/Product/AddImages/id/1008
同樣,以下也適用:第二路由
http://localhost:1234/Product/AddImages/1008
但我想迫使網址顯示或寫在地址欄中的URL作爲第一個面向特定頁面「AddImages」是這樣的:http://localhost:1234/Product/AddImages/id/1008
有沒有辦法做到這一點我的意思是保持默認路由和特定頁面的第一個路由選項?
同樣,我試圖從第一個網址檢索查詢字符串如下:
int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); //This works for the second routing option
它說 - 輸入字符串的不正確的格式。我想,這是默認的路由選擇,因爲它的配置爲:
url: "{controller}/{action}/{id}"
再次,是它可以從以下網址獲得查詢字符串值或配置任何東西:
http://localhost:1234/Product/AddImages/id/1008
您的航線順序錯誤 - Default2必須在第一位(默認值之前) –
@Stephen Muecke我可以通過對路線進行排序來解決問題。感謝你和無腦編碼器的想法。 –