2014-12-02 37 views
0

我是新來asp.net和正在學習的URL路由URL路由,如Facebook asp.net

在Facebook的鏈接(https://www.facebook.com/james.wood)怎麼做james.wood工作?

我試圖通過使用此代碼添加在做在我的asp.net同樣的事情,我的全球

route.MapPageRoute("Profile", "epubtest/profile/{profileid}", "~/epubtest/Profile.aspx"); 

,但我不能使它與工作就像james.wood

是否點手段另一個參數還是隻有一個參數?

它的工作原理沒有點上它只是一個字

+1

當你用點測試時會發生什麼? – haim770 2014-12-02 16:48:35

+0

@ haim770它將註冊爲一個新頁面。並且我得到了'HTTP錯誤404.0 - 未找到' – 2014-12-02 16:49:08

+0

它可能將'.wood'看作是一個擴展 – DGibbs 2014-12-02 16:50:04

回答

0

1)您可以添加這條路線:

routes.MapRoute(
    name: "User", 
    url: "{username}", 
    defaults: new { controller = "Destiny", action = "Index" }, 
    constraints: new { username = new UserNameConstraint() } 
); 

2)創建這個類:

public class UserNameConstraint : IRouteConstraint 
{ 
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
    { 
     List<string> users = new List<string>() { "username1", "username2" }; 

     var username = values["username"].ToString().ToLower(); 

     return users.Any(x => x.ToLower() == username); 
    } 
} 

3)DestinyController

public class DestinyController : Controller 
{ 
    public ActionResult Index(string username) 
    { 
     return View(); 
    } 
} 

我h我幫助過。 擁抱!