Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing RequestContext from global.asax

Does anyone know how to get the current RequestContext from the Application_Error event in global.asax?? My problem is that i need to do a redirect, and thereby need to have the url generated using UrlHelper - which takes the aformentioned RequestContext.

like image 894
Christian Nielsen Avatar asked Mar 30 '09 10:03

Christian Nielsen


2 Answers

While there is no direct way of accessing the RequestContext, you can create one yourself:

RequestContext context = new RequestContext(new HttpContextWrapper(HttpContext.Current), RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)))

So the UrlHelper can be constructed via:

UrlHelper helper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current))));

Not pretty, but it gets the job done.

like image 67
Gene Hallman Avatar answered Nov 13 '22 15:11

Gene Hallman


You can access the request context using

HttpContext.Current.Request.RequestContext

Or, if you're in the Global.asax you can use

Context.Request.RequestContext

directly.

like image 39
Medeni Baykal Avatar answered Nov 13 '22 14:11

Medeni Baykal