动态ASP.NET MVC路由

时间:2010-11-03 17:31:33

标签: asp.net-mvc asp.net-mvc-routing

我对MVC路线感到困惑。假设我有这个域名:poopoopants.com。我想要根目录下的一些标准网址转到“关于”页面或“联系”页面:

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

现在,我将在下面的这两条路线中拥有无限量,其中“[user]”是在poopoopants.com上注册帐户的人的用户名变量,[file-path] ]是一个URL清理文件路径,可能包含/字符

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

所以我假设我有一个IndexController Index行动/,行动About/about行动Contact } /contact的操作,以及UserController的{​​{1}} Index操作/[user]/File的{​​{1}}操作。

我的问题涉及明确/[user]/[file-path]中的路线。到目前为止我已经:

Global.asax.cs

但我应该为/ [user]和/ [user] / [file-path]路由指定什么,以及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" }); 中其操作的相应方法签名是什么?

1 个答案:

答案 0 :(得分: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子句中创建索引控制器Route。

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