我正在構建一個asp.net mvc網站,用戶登錄後他可以訪問他的個人資料部分頁面,並且當前這些網頁的URL類似於www.example.com/profile,什麼我想要的是使這樣的網址www.example.com/USERNAME如何編寫包含用戶名的路由
如何編寫這條路線,只有在用戶登錄時才能在配置文件頁面中工作?
更新:
基於下面的答案,我寫的是這樣的:
routes.MapRoute(
"AccountSettins",
"AccountSettings",
new { controller = "AccountSettings", action = "Index" }
);
routes.MapRoute(
"myRouteName",
"{username}",
new { controller = "Profile", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
和控制器:
[Authorize]
public class ProfileController : BaseController
{
//
// GET: /Profile/
public ActionResult Index(string username= "")
{ ...
但現在用戶登錄之後他用戶名是「xyz」,他可以訪問www.example.com/xyz,這將導致個人檔案頁面,但如果他也寫了網址www.example.com/abc,他會通常從用戶的角度來看一些奇怪的東西,如何解決這個問題?
,但喜歡它認爲任何URL是一個用戶名,假設我希望用戶去www.example.com/帳戶設置它認爲帳戶設置是用戶名 –
我想我需要爲此創建另一條路由。 –
'routes.MapRoute( 「AccountSettins」, 「AccountSettings」, new {controller =「AccountSettings」,action =「Index」} ); 路線。MapRoute( 「myRouteName」, 「{username}」, new {controller =「Profile」,action =「Index」} ); routes.MapRoute( 「Default」,//路由名稱 「{controller}/{action}/{id}」,//帶參數的網址 新{controller =「Home」,action =「Index」, id = UrlParameter.Optional} //參數默認值' –