简体   繁体   中英

Route URL such as http://www.example.com/{userId}

I would like my MVC website to be able to route the following URLs:

http://www.example.com/ {userId}
http://www.example.com/ {userId}/enroll

Note: userId is a string of letters, numbers and/or hyphens.

I realize this is problematic because the URL does not specify a controller name. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM