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);

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.