How can I resolve ASP.NET "~" app paths to the website root without a Control being present?

John K picture John K · Apr 7, 2010 · Viewed 21.3k times · Source

I want to Resolve "~/whatever" from inside non-Page contexts such as Global.asax (HttpApplication), HttpModule, HttpHandler, etc. but can only find such Resolution methods specific to Controls (and Page).

I think the app should have enough knowledge to be able to map this outside the Page context. No? Or at least it makes sense to me it should be resolvable in other circumstances, wherever the app root is known.

Update: The reason being I'm sticking "~" paths in the web.configuration files, and want to resolve them from the aforementioned non-Control scenarios.

Update 2: I'm trying to resolve them to the website root such as Control.Resolve(..) URL behaviour, not to a file system path.

Answer

Max Toro picture Max Toro · Apr 7, 2010

Here's the answer: ASP.Net: Using System.Web.UI.Control.ResolveUrl() in a shared/static function

string absoluteUrl = VirtualPathUtility.ToAbsolute("~/SomePage.aspx");