在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中進行設(shè)置時,URL不會更改。其次,我無法導(dǎo)航到其他控制器。如果我將URL更改為http:// localhost123 / Welcome,則不會發(fā)生任何事情。它應(yīng)該將我重定向到新頁面。我在這里做錯了什么?如果更改路線順序,則可以導(dǎo)航到其他頁面,但用戶名未顯示在URL中。我已經(jīng)用谷歌搜索,關(guān)于這個問題的所有答案都說我應(yīng)該使用上述路線。
3 回答

偶然的你
TA貢獻1841條經(jīng)驗 獲得超3個贊
您需要對網(wǎng)站不同部分的url進行分類,以使url模式匹配機制順利進行。例如,在您的情況下,輸入類別“個人資料”或其他任何內(nèi)容?,F(xiàn)在您的請求網(wǎng)址看起來像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條經(jīng)驗 獲得超3個贊
如果我可以投票兩次,我會的。當我意識到這一點時,我總是會忘記它的束縛和愚蠢。我的腦海立刻跳到我很久以前做過的瘋狂駭客,每當我回頭看看它時,都在思索。
- 3 回答
- 0 關(guān)注
- 576 瀏覽
添加回答
舉報
0/150
提交
取消