2009-07-21 60 views
3

我想知道是否有反正在MVC中實現像http://www.mycompany.com/user這樣的網址 我試過使用catch全部但不能讓用戶通過,所以我可以查找。URL路由全部收錄

謝謝

+1

你能澄清你的意思是什麼'無法讓用戶通過'?你是在談論路線的「用戶」部分還是你期望的其他一些數據?這可能是一個路由問題,建立一個默認路由,或一個處理特定場景,並不是太困難。 – nkirkes 2009-07-21 18:04:40

+0

如果網址是http://www.mycompany.com/user 我期望被路由到這個ActionResult public ActionResult Index(string userName) { //這個userName參數在它應該是//字符串用戶 UserModel model = Data.GetUser(userName); return查看(model); } – Sammy 2009-07-21 18:30:53

回答

1

這樣的事情?

routes.MapRoute("User", 
    "{UserName}", 
    new { Controller = "User", Action = "Index", UserName = "" }); 

更新:

此約束添加到 「用戶」 路線:

routes.MapRoute("User", 
    "{UserName}", 
    new { Controller = "User", Action = "Index", UserName = "" }, 
    new { UserName = @"(\w|-)+" } 
); 

或添加這條路線:

routes.MapRoute("Home", 
    String.Empty, 
    new { Controller = "Home", Action = "Index", Id = "" } 
); 
+0

剛剛嘗試過,現在「使用本地主機」現在請求默認路由「http:// localhost/MVCApp /」被重定向到用戶控制器,用戶名爲空字符串 – Sammy 2009-07-21 18:48:48

+0

您擊敗了我到約束解決方案。 非常感謝你 – Sammy 2009-07-21 19:06:29

+0

不客氣:) – 2009-07-21 19:13:56