views:

111

answers:

3

I'd like to parse an incoming URL into it's component parts for some pre-processing before passing it into the standard MVC routing logic. For example given your standard route

{controller}/{action}/{id}

and the URL

/user/show/10

Is there a way to have the Routing system return a dictionary containing controller, action and id keys with their corresponding values?

A: 

MVC already does this for you. You can access the RouteData property inside the controller.

I could probably answer better if you defined where you'd like to intercept the route data before the MVC routing logic.

jfar
I've considered this but I'm currently working with the url in the HttpApplication.BeginRequest method - which I believe is invoked before the routing system has a chance to get at the URL. I was hoping there was some way to simply invoke some routing method and just have it return a RouteData from the url.
Paul Alexander
A: 

Have you tried splitting the string on the '/' character? That would give you an ordinal array of the path parts.

var parts = url.substring(1).Split('/');
var controller = parts[0];
var action = parts[1];
var id = parts[2];
Payton Byrd
I really need to see it how the routing system sees it - not just the URL broken apart. Some of the routes in use have multiple parameters so url splitting won't quite work.
Paul Alexander
+1  A: 

It's pretty simple, the routing system is initialized at app start, and it calculates the route data based on the current context, so anytime the context itself is current, you're all good.

In the Application_BeginRequest method of Global.asax, for example, you can get the current route data like so:

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

The routedata in turn has the RouteValueDictionary for the current request stored in its Values property and it has the data tokens stored in the DataTokens property.

Paul

Paul
I'll have to schedule some time to check this out, but it looks like exactly what I'm after.
Paul Alexander