我想弄清楚如何枚舉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