I’m trying to use VirtualPathUtility.ToAbsolute to resolve app-relative paths, such as ~/MyPage.aspx, to application-absolute paths, such as /MySite/MyApp/MyPage.aspx. However, with some paths, I receive an HttpException saying that my path is “not a valid virtual path”. Examples:
// This works:
var abs1 = VirtualPathUtility.ToAbsolute("~/MyPage.aspx#anchor");
// This errors:
var abs2 = VirtualPathUtility.ToAbsolute("~/MyPage.aspx?key=value");
What’s going on?
Because you’re using .NET 3.5, you’re using the
2.0System.Webassembly, which has the defect that?is considered an illegal path character by this method. This is mentioned in the community comments on the version-specific MSDN page.By disassembling, it can be seen that the call ends up in the (
internal)VirtualPath.Create, which has:which references
Some of these can reasonably be regarded as bad characters for a path, but
?shouldn’t really be so rejected.Disassembly of the
4.0System.Webshows thatVirtualPath.Createhas been rewritten to be more discerning.This web.archive capture of a now-defunct blogs.msdn post shows one of the earliest mentions of this problem. The MS employee responds:
where
ResolveAppRelativeLinkToUrlrefers to the reporter’s code’s method name.Another workaround would be to replace
?with a safe token before the call toVirtualPathUtility.ToAbsolute, and reverse the replacement afterwards:choosing a suitably unlikely token for your application.