You can use this code anywhere, and you don't need the UrlHelper or the context:
RouteValueDictionary rvd = new RouteValueDictionary
{
{"controller", "ControllerName"},
{"action", "ActionName"},
{"area", ""}
};
VirtualPathData virtualPathData
= RouteTable.Routes.GetVirtualPath(null, new RouteValueDictionary());
return virtualPathData.VirtualPath;
This will only work if the property 'HttpContext.Current' is not null, which is true for code running in a web app. If you don't supply the first parameter for GetVirtualPath the system will find use the context returned by the aforesaid static property.
So it's still true that you can use this method to get the URL without supplying the context.
The context is always necessary because it has the information of the server, and virtual directoy if present, in which the app is running.
HtmlHelper<TModel>extensions?