【发布时间】:2010-12-14 18:28:10
【问题描述】:
我正在尝试弄清楚如何在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?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-routing