路线网址,例如http://www.example.com/{userId}

时间:2017-04-02 18:04:35

标签: c# asp.net asp.net-mvc routes

我希望我的MVC网站能够路由以下网址:

  

http://www.example.com/ {用户id}
  http://www.example.com/ {userId} / enroll

注意:userId是一串字母,数字和/或连字符。

我意识到这是有问题的,因为URL没有指定控制器名称。但是,理论上应该是可能的。我可以在我的申请中use reflection to get a list of all the controllers。如果{userId}与任何控制器都不匹配,则应将其重定向到特定的控制器/操作。

我的第一个问题是你如何指定这样的地图?我可以指定一个字符串值,我甚至可以指定一个正则表达式。但是如何根据它是否与字符串列表匹配来进行过滤?

除此之外,只是想知道是否有其他人想过做事,以及他们是否有任何其他有关如何实现它的创意。

1 个答案:

答案 0 :(得分:3)

如果我理解你的问题:

routes.MapRoute(
  name: "Default",
  url: "{id}/{action}",
  defaults: new { controller = "Custom", action = "Index" },
  constraint: new { id = new MyRouteConstraint()});

在自定义MyRouteConstraint: IRouteConstraint中,您可以将参数与字符串列表匹配

相关问题