Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

Access RouteData for the current Route from a library class in asp.net mvc October 7, 2009

Filed under: Asp.Net,C#,MVC — Rupert Bates @ 1:20 pm

Sometimes it’s useful to be able to access the current RouteData from outside a controller, for instance in a library class. In webforms you could always do


HttpContext.Current

but in Asp.Net MVC it’s a bit less obvious how you get access to this data.

I’ve found the following code works:


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

Whether there is an easier way or not I don’t know, but this will do me for now…

Advertisements
 

5 Responses to “Access RouteData for the current Route from a library class in asp.net mvc”

  1. […] to VoteAccess RouteData for the current Route from a library class in asp.net mvc (10/7/2009)Wednesday, October 07, 2009 from eliasbland.wordpress.comCurrent but in Asp. Net MVC it’s a bit […]

  2. rykneethling Says:

    Thanks, just what i needed.

  3. Levitikon Says:

    Exactly what I needed!

  4. yaron levi Says:

    This method might hit performance.
    Use HttpContext.Current.Request.RequestContext.RouteData instead
    http://stackoverflow.com/questions/976855/how-can-you-access-routedata-from-the-code-behind

  5. OlegASD Says:

    Thanks a lot!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s