简体   繁体   English

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

[英]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}/enroll http://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.

相关问题 如何将非www或非http重定向到https://www.example.com - How to redirect non www or non http to https://www.example.com 如何将www.example.com重定向到example.com? - How to redirect www.example.com to example.com? window.setTimeout('window.location =“ http://www.example.com”;',2000); 在后面的代码中 - window.setTimeout('window.location=“http://www.example.com”; ', 2000); in code behind 如何从http://www.example.com/images/business/113.jpg获取数字 - How to get numbers from http://www.example.com/images/business/113.jpg 如何使用 articleName=sample-article-name [MVC Core2.2] 将“www.example.com/sample-article-name”路由到 RazorPage Index.cshtmll - How to route to "www.example.com/sample-article-name" to RazorPage Index.cshtmll with articleName=sample-article-name [MVC Core2.2] 捕获和识别HttpRequestException“无法解析远程名称:'www.example.com'”的正确方法是什么? - What is the proper way to catch and identify the HttpRequestException “The remote name could not be resolved: 'www.example.com'”? 将所有来自www.example.com的流量重定向到IIS7中的特定页面 - redirect all traffic coming from www.example.com to specific page in IIS7 如何获取http://www.google.com网站的重定向网址 - How to get redirect url of http://www.google.com site 像http://example.com这样的输入网址在操作输入中更改为http:/example.com - Input URL like http://example.com changes to http:/example.com in action input 在MVC控制器中验证Webhook调用http:// secretkey:12345@www.example.com - Authenticating a webhook call in mvc controller http://secretkey:12345@www.example.com
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM