我正在尝试找出如何枚举RouteTable
中Routes
的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
?