2012-03-25 84 views
1

我正在構建一個小型的MVC應用程序。之後用戶登錄我想他/她顯示路線:使用用戶名MVC MapRoute用戶名

www.appname.com/username/

底下當然同樣的動作被稱爲爲每個用戶如/home/index。我如何編寫我的MapRoute來實現這個目標?我應該使用哪些其他代碼(屬性)?

+0

什麼字符在用戶名中被授權?例如,你允許用戶在你的網站上註冊username = Home/Index嗎? – 2012-03-25 15:42:36

+0

@Darin Dimitrov,用戶名稱將不會有特殊字符。我主要是爲了學習目的而構建它,所以沒有太複雜的東西,只想讓用戶登錄並看到可以與其他人共享的自定義網址。 – mishap 2012-03-25 16:21:13

回答

1

此擊潰添加到您的航線global.asax.cs文件

routes.MapRoute(
      "RouteName", // Route name 
      "FixedUrlSegment/{UserName}/{Controller}/{action}/{id}/", // URL with parameters 
      new { controller = "ControllerName", 
        action = "ActionName", 
        id=UrlParameter.Optional 
       }, 
     ); 

我認爲你應該使用一個固定段的啓動點,你的路線,從默認或其它途徑

當然

區別在日誌中的操作方法,您必須重定向到新的路由

return RedirectToRoutePermanent("RouteName", new { username = "UserName", 
                action = "Index", 
                controller = "Home", 
                id="userId" 
               } 
           ); 
// remember id is not required for that route as mentioned in global file 

這個例子將重定向你的網頁的URL

www.appname.com/FixedUrlSegment/loggedusername/home/index/loggeduserid