ASP.Net - How to get an HttpContextBase object


In using the friendly URL routing in ASP.Net I ran into an issue trying to get the route data out at runtime. The GetRouteData method required an HttpContextBase which is not the same as the current HttpContext. In order to get the HttpContextBase you can use the HttpContextWrapper and pass it the current context.

VB.Net

Dim wrapper As HttpContextWrapper = New HttpContextWrapper(HttpContext.Current)           
Dim rd As RouteData = r.GetRouteData(wrapper)

C#

var wrapper = new HttpContextWrapper(HttpContext.Current);
var rd = r.GetRouteData(wrapper);