ASP.Net - How to get an HttpContextBase object

Posted by Blake on 11/4/2014
)

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#

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