Using the greedy route parameter in the middle of a route definition

南楼画角 提交于 2019-12-01 14:03:59

Routes ignore query string parameters. But at the same time, query string parameters are passed in to an action method as long as there isn't a route URL parameter of the same name. So I would use just the second route, and pass in title via the query string.

Another option is more complicated. You write a custom route that derives from Route and override the GetRouteData method so that it parses the value of "categories" (something like RouteData.Values["categories"] and then add the parsed data to the route value dictionary (RouteData.Values["title"] = parsedTitle.

Greedy segment anywhere in the URL

I've written GreedyRoute class that supports greedy (catch all) segment anywhere in the URL. It's been a while since you needed it, but it may be useful to others in the future.

It supports any of the following patterns:

  • {segment}/{segment}/{*greedy} - this is already supported with default Route class
  • {segment}/{*greedy}/{segment} - greedy in the middle
  • {*greedy}/{segment}/{segment} - greedy at the beginning

You can read all the details on my blog post and get the code as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!