2010-11-03 154 views
1

我对MVC路由感到困惑。假设我有这个域名:poopoopants.com。我想一些标准的网址,关闭根说去一个“关于”页面或“联系方式”页面:动态ASP.NET MVC路由

http://poopoopants.com/about 
http://poopoopants.com/contact 

现在,我也将有下面这两条路的无限量,其中“[用户]”是用户名,用于与poopoopants.com帐户注册的人,和[文件路径]的变量是一个网址的sanitize文件路径可含有/字符

http://poopoopants.com/[user] 
http://poopoopants.com/[user]/[file-path] 

所以我假设我会有一个IndexControllerIndex行动/,About a​​,Contact动作为/contact,一个为UserControllerIndex动作为/[user]/File动作为/[user]/[file-path]

我的问题涉及到Global.asax.cs中的路线。到目前为止,我已经有了:

routes.MapRoute("Index", "/", new { controller = "Index", action = "Index" }); 
routes.MapRoute("About", "/about", new { controller = "Index", action = "About" }); 
routes.MapRoute("Contact", "/contact", new { controller = "Index", action = "Contact" }); 

但是我指定/ [用户]和/ [用户]/[文件路径]路线,以及他们在UserController行动的相应方法签名?

回答

2

我没有测试,但它应该工作:

路线:

routes.MapRoute(
    "User", 
    "{username}", 
    new { controller = "User", action = "Index" } 
); 

routes.MapRoute(
    "File", 
    "{username}/{filepath}", 
    new { controller = "User", action = "File" } 

方法签名:

public ActionResult Index(string username); 
public ActionResult File(string username, string filepath); 

此外,您还可以让你的指数控制器路线可能是在一个MapRoute子句。

有用的工具:http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

+1

我不得不改变URL参数“文件”路线“{用户名}/{*}文件路径”支持文件路径以“/”在其中,但工作perfectly- -谢谢! – SnickersAreMyFave 2010-11-03 18:24:55

+0

嗯,我没有看到我可以如何为联系人和关于一条路由,因为用户路由将捕获任何未明确定义的路由,对吧? – SnickersAreMyFave 2010-11-03 18:26:30

+0

是的,我错了。快速测试证明;)很高兴我可以帮助你,与你的项目祝你好运。 – mlusiak 2010-11-03 21:43:16