在ASP.NET MVC中路由,在URL中顯示用戶名我正在嘗試建立一條路線,以便可以在URL中顯示用戶名,如下所示:HTTP:// localhost1234 /約翰·這是我的routeconfig: routes.MapRoute(
name: "users", // Route name
url: "{username}", // URL with parameters
defaults: new { controller = "Home", action = "Index", username = "" } // Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);這是我的HomeController: public ActionResult Index(string username = "Test")
{
return View();
}首先,URL不變。當我username = "Test"在route-config中進行設置時,URL不會更改。其次,我無法導航到其他控制器。如果我將URL更改為http:// localhost123 / Welcome,則不會發生任何事情。它應該將我重定向到新頁面。我在這里做錯了什么?如果更改路線順序,則可以導航到其他頁面,但用戶名未顯示在URL中。我已經用谷歌搜索,關于這個問題的所有答案都說我應該使用上述路線。
3 回答
偶然的你
TA貢獻1841條經驗 獲得超3個贊
您需要對網站不同部分的url進行分類,以使url模式匹配機制順利進行。例如,在您的情況下,輸入類別“個人資料”或其他任何內容?,F在您的請求網址看起來像http:// localhost1234 / profile / john,而路由將是
routes.MapRoute(
name: "users", // Route name
url: "Profile/{username}", // URL with parameters
defaults: new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
德瑪西亞99
TA貢獻1770條經驗 獲得超3個贊
如果我可以投票兩次,我會的。當我意識到這一點時,我總是會忘記它的束縛和愚蠢。我的腦海立刻跳到我很久以前做過的瘋狂駭客,每當我回頭看看它時,都在思索。
- 3 回答
- 0 關注
- 594 瀏覽
添加回答
舉報
0/150
提交
取消
