Is it possible to parse a url into composite components?

I would like to parse the incoming URL into its constituent parts for some preprocessing before passing it to the standard MVC routing logic. For example, given the standard route

{controller}/{action}/{id}

and url

/user/show/10

Is there a way for the routing system to return a dictionary containing the keys of the controller, action, and identifier with the corresponding values?

+3
source share
3 answers

It’s quite simple, the routing system is initialized when the application starts and calculates the route data based on the current context, so at any time when the context itself is relevant, you are fine.

Application_BeginRequest Global.asax, , :

var routedata = RouteTable.Routes.GetRouteData(new HttpContextWrapper(Context));

, routingata RouteValueDictionary , , , DataTokens.

+1

MVC . RouteData .

, , MVC.

0

'/'? .

var parts = url.substring(1).Split('/');
var controller = parts[0];
var action = parts[1];
var id = parts[2];
0

Source: https://habr.com/ru/post/1736810/


All Articles