URL路由捕获所有

时间:2009-07-21 17:41:56

标签: asp.net-mvc

我想知道是否还有在MVC中获得类似http://www.mycompany.com/user的网址 我尝试使用catch all,但无法让用户通过,所以我可以查看。

由于

1 个答案:

答案 0 :(得分: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 = "" }
);