2012-10-27 39 views
0

我有一個像路線的動態的用戶名路徑下用戶的頁面中MVC4

routes.MapRoute(
      "UserNames", // Route name 
      "{username}", // URL with parameters 
      new { controller = "Home", action = "UserName" }); 

和下HomeController.cs

public ActionResult UserName(string username) 
     { 
      ViewBag.Message = username; 

      return RedirectToAction("Register","Account"); // Test... 
     } 

它做工精緻一些動態用戶路線。

但我需要的是讓工作像

http:\\mywebsite.com\UserNameBob\MyGallery\1 
http:\\mywebsite.com\UserNameBob\Profile 
http:\\mywebsite.com\UserNameBob\MyFriends 

的URL我如何可以存檔嗎?

任何clu?

謝謝!!!

回答

2

你的意思是這樣的:

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

然後在HomeController你把這樣的動作:

public ActionResult MyGallery(string username, int id) { 
    // code 
} 

public ActionResult Profile(string username) { 
    // code 
} 

編輯:當然,如果畫廊ID不是一個整數,只是使用string或任何適當的。

+0

我有沒有保留已經擁有或使用過你的路線? –

+0

我應該替換你的,加上你在動作參數中顯然需要用戶名。我會編輯答案。 – 2012-10-27 13:31:50

0

在ASP.NET中查找URL Rewriting以處理路由時的動態參數。

+0

對不起,但我問它的ASP .NET MVC ... –