枚举 ASP.NET MVC RouteTable 路由 URL
我试图弄清楚如何枚举 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");
那么应该抛出异常。我需要用什么来替换 //...确保 'name' 不是根目录下的路由 URL
?
I'm trying to figure out how to enumerate the URLs of Routes
in the RouteTable
.
In my scenario, I have the following routes defined:
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" });
In other words, if my site is mysite.com, mysite.com/create invokes PadController.CreateNote()
, and mysite.com/foobaris invokes PadController.Index()
.
I also have a class that strongly types usernames:
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;
}
}
In the constructor for Username
, I would like to check to make sure that name
isn't a defined route. For example, if this is called:
var username = new Username("create");
Then an exception should be thrown. What do I need to replace //... make sure 'name' isn't a route URL off root
with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不能完全回答您想要通过阻止用户注册受保护的单词来做什么,但是有一种方法可以限制您的路线。我们的网站中有 /username url,并且我们使用了这样的约束。
您可能只需要保留保留字列表,或者,如果您确实希望它自动进行,您可以使用反射来获取名称空间中的控制器列表。
您可以通过它访问路线集合。这种方法的问题在于,它要求您显式注册您想要“保护”的所有路由。我仍然坚持我的说法,最好将保留关键字列表存储在其他地方。
This doesn't fully answer what you are wanting to do by preventing users from registering protected words, but there is a way you can constrain your routes. We had /username url's in our site and we used a constraint like so.
You may just have to keep a list of reserved words, or, if you really want it automatic, you could possibly use reflection to get a list of the controllers in the namespace.
You can access the route collection with this. The issue with this approach is that it requires you to explicitly register all routes you want to be "protected". I still hold to my statement you'd be better off having a list of reserved keywords stored elsewhere.