views:

398

answers:

2

When using ASP.Net routing, how can you get the RouteData from the code-behind?

I know you can get it from the GetHttpHander method of the RouteHandler (you get handed the RequestContext), but can you get this from the code-behind?

Is there anything like...

RequestContext.Current.RouteData.Values["whatever"];

...that you can access globally, like you can do with HttpContext?

Or is it that RouteData is only meant to be accessed from inside the RouteHandler?

A: 

I think you need to create a RouteHandler then you can push the values into HTTPContext during the GetHttpHandler event.

foreach (var urlParm in requestContext.RouteData.Values) { requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value; }

You can find more information in this MSDN article.

JD
+1  A: 

You can use the following:

RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));
Rupert Bates