[英]Route URL such as http://www.example.com/{userId}
I would like my MVC website to be able to route the following URLs: 我希望我的MVC网站能够路由以下URL:
http://www.example.com/ {userId}
http://www.example.com/ {userId}
http://www.example.com/ {userId}/enrollhttp://www.example.com/ {userId} / enroll
Note: userId
is a string of letters, numbers and/or hyphens. 注意:
userId
是一个字母,数字和/或连字符的字符串。
I realize this is problematic because the URL does not specify a controller name. 我意识到这是有问题的,因为URL没有指定控制器名称。 However, it should be possible in theory.
但是,理论上应该是可能的。 I can use reflection to get a list of all the controllers in my application.
我可以使用反射来获取应用程序中所有控制器的列表 。 If
{userId}
does not match any of those controllers, then it should be redirected to a particular controller/action. 如果
{userId}
与这些控制器中的任何一个都不匹配,则应将其重定向到特定的控制器/操作。
My first question is how would you specify a map like this? 我的第一个问题是你如何指定这样的地图? I can specify a string value, I can even specify a regular expression.
我可以指定一个字符串值,我甚至可以指定一个正则表达式。 But how can I filter based on whether or not it matches a list of strings?
但是如何根据它是否匹配字符串列表进行过滤?
Beyond that, just wondered if anyone else has thought of doing things and if they have any other creative ideas on how I might accomplish it. 除此之外,只是想知道是否有其他人想过做事,以及他们是否有任何其他创造性的想法,我将如何实现它。
If I understand your question right: 如果我理解你的问题:
routes.MapRoute(
name: "Default",
url: "{id}/{action}",
defaults: new { controller = "Custom", action = "Index" },
constraint: new { id = new MyRouteConstraint()});
In custom MyRouteConstraint: IRouteConstraint
you can match your parameter with list of strings 在自定义
MyRouteConstraint: IRouteConstraint
您可以将参数与字符串列表进行匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.