我想弄清楚如何枚举Routes
的URL在RouteTable
。枚举ASP.NET MVC RouteTable路由URL
在我的情况,我有以下途径定义:
routes.MapRoute
("PadCreateNote", "create", new { controller = "Pad", action = "CreateNote" });
routes.MapRoute
("PadDeleteNote", "delete", new { controller = "Pad", action = "DeleteNote" });
routes.MapRoute
("PadUserIndex", "{username}", new { controller = "Pad", action = "Index" });
换句话说,如果我的网站是mysite.com,mysite.com/create调用PadController.CreateNote()
和mysite.com/foobaris调用PadController.Index()
。
我也有一类是强类型的用户名:
public class Username
{
public readonly string value;
public Username(string name)
{
if (String.IsNullOrWhiteSpace(name))
{
throw new ArgumentException
("Is null or contains only whitespace.", "name");
}
//... make sure 'name' isn't a route URL off root like 'create', 'delete'
this.value = name.Trim();
}
public override string ToString()
{
return this.value;
}
}
在构造为Username
,我想检查,以确保name
不是定义路线。例如,如果这被称为:
var username = new Username("create");
然后应该抛出一个异常。我需要用什么替换//... make sure 'name' isn't a route URL off root
?
向给定路线的DataTokens添加'受保护的'布尔值不是不合理的。不一定建议,但管理起来并不困难。 – 2010-12-14 19:31:36