Próbuję wymyślić, jak wyliczyć adresy URL Routes
w RouteTable
.Wyliczanie adresów URL trasy ASP.NET MVC RouteTable
W moim scenariuszu Mam następujące trasy zdefiniowane:
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" });
Innymi słowy, jeśli moja strona jest mysite.com, mysite.com/create wywołuje PadController.CreateNote()
i mysite.com/foobaris wywołuje PadController.Index()
.
Mam także klasę silnie typy nazwy użytkownika:
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;
}
}
w konstruktorze dla Username
, chciałbym sprawdzić, aby upewnić się, że name
nie jest określona trasa. Na przykład, jeśli jest to nazywane:
var username = new Username("create");
Następnie należy rzucić wyjątek. Co muszę zastąpić //... make sure 'name' isn't a route URL off root
?
Dodanie "chronionej" wartości logicznej do DataTokens dla danej trasy nie byłoby nieuzasadnione. Niekoniecznie sugeruje to, ale nie byłoby to szczególnie trudne. –