2016-03-03 12 views
0

我編碼一個網站就像一個平臺,讓我們可以從以下網址訪問用戶配置文件的路徑:如何創建www.website.com/profile

www.mywebsite.com/DanielVC

有關於外形細節控制器是以下

  • 控制器:PERFIL
  • 行動:PERFIL

我已經有了以下的路徑,爲所有的應用程序:

routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional } 
); 

我已經嘗試過創建以下路線:

routes.MapRoute(
    name: "Perfil", 
    url: "Pages/{id}", 
    defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional } 
); 

回答

2

將這個沒有工作在(上方)默認路線之前。

routes.MapRoute(
    name: "Perfil", 
    url: "{id}", 
    defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional } 
); 

而且,這將導致在每一個被重定向到PERFIL路線的路線。如果找不到用戶名(例如mywebsite.com/randomuserthatdoesntexist)和/或其他路線(mywebsite.com/contact),則必須在該操作中創建重定向。

編輯

實例爲你的方法

public class PagesController : Controller 
{ 
    public ActionResult Index(string id) 
    { 
     if (matchesOtherRoute(id)) 
      RedirectToAction("OtherAction", "OtherController"); 
     if (!userExists(id)) 
      RedirectToAction("NotFoundAction", "ErrorController"); 
     // Do other stuff here 
    } 
} 
0

應該像 routes.MapRoute( 名稱: 「PERFIL」, 網址: 「頁/(編號)」, 默認值:new {controller =「Perfil」,action =「Perfil」,id = UrlParameter.Optional} );