2011-08-19 109 views
1

我正在構建一個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,他會通常從用戶的角度來看一些奇怪的東西,如何解決這個問題?

回答

2

在您的Global.asax ...

routes.MapRouteWithName(
    "routeUserProfile", 
    "{username}", 
    new { controller = "User", action = "Profile", username = "" }); 

在您的用戶控制器中......

public ActionResult Profile(string username) { 
    //conditional logic to check if username is user 
    // render user profile with special user-only stuff 
    //else 
    // render only generic stuff about user 
} 
+0

,但喜歡它認爲任何URL是一個用戶名,假設我希望用戶去www.example.com/帳戶設置它認爲帳戶設置是用戶名 –

+0

我想我需要爲此創建另一條路由。 –

+0

'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} //參數默認值' –

1
routes.MapRoute(
    "myRouteName", 
    "{username}", 
    new { controller = "Home", action = "Profile" } 
    ); 

您可以指定控制器,並且希望採取行動,只是使用的用戶名的參數爲家庭類的方法簡介。

+0

請參閱我的更新問題與代碼示例 –

1

您將需要專門編寫的控制,這和創建這樣一個路徑:

routes.MapRoute(
    "UserPage", // Route name 
    "{username}", // URL with parameters 
    new { controller = "User", action = "Index", username = ""} // Parameter defaults 
); 

在這裏看到更多的細節: http://dotnet.dzone.com/articles/aspnet-mvc-routing-basics?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fdotnet+(.NET+Zone

+0

請參閱我的更新問題與代碼示例 –

1

在Global.asax文件中添加以下途徑

 routes.MapRoute(
      "UsersRoute", // Route name 
      "{username}", // URL with parameters 
      new { controller = "Test", action = "Index", username = "" } 
     ); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 

而根據第一路由添加以下控制器波紋管

public class TestController : Controller 
{  
    public ActionResult Index(string username) 
    { 
     var p = username; 
     return View(); 
    } 
} 
1

爲了防止用戶查看他人個人資料,只檢查行動,如果他/她可以這樣做。

public ViewResult Index(string username) 
{ 
    if (CanSeeOthersProfiles(username)) //your function to check currently logged user and his privileges 
    { 
    var model = new MyModel(); 
    //do your logic 
    return View(model); 
    } 
    else 
    return RedirectToAction("index", "home"); 
} 
相關問題