Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you access RouteData from the code-behind?

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?

like image 285
Deane Avatar asked Jun 10 '09 16:06

Deane


People also ask

What is RouteData?

RouteData is an attribute of the basic Controller class, so you can access RouteData in any controller. RouteData contains routing information for the current request. You can use RouteData to get controller, operation or parameter information as shown below.

What is attribute routing in C#?

As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API. For example, you can easily create URIs that describe hierarchies of resources. The earlier style of routing, called convention-based routing, is still fully supported.


2 Answers

You could also use the following:

//using System.Web; HttpContext.Current.Request.RequestContext.RouteData 
like image 149
Hosam Aly Avatar answered Sep 22 '22 18:09

Hosam Aly


You can use the following:

RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)); 
like image 40
Rupert Bates Avatar answered Sep 22 '22 18:09

Rupert Bates