Wednesday 18 August 2010

Retrieving Actual .aspx File Path when using Url Routing

In the last few days I was almost driven to madness, trying to get in code the Path of the actual .aspx file servicing a request that was routed trough the new ASP.NET 4.0 URL Routing. The problem was that I wanted to retrieve a LocalResouce for custom control from the page resource files. After a lot of searching, I managed to find a way myself.

System.String Path = ((System.Web.Routing.PageRouteHandler)(((System.Web.Routing.Route)(HttpContext.Current.Request.RequestContext.RouteData.Route)).RouteHandler)).VirtualPath;
then to get the LocalResourceObject you do:
 System.String localResource = HttpContext.GetLocalResourceObject(path, LocalResourceName).ToString();

I hope this helps some of you.