I have the following helper method:
public static String ResolveUrl(String url)
{
url = url.Trim();
if (!(url[0] == '~')) return url;
try
{
if (VirtualPathUtility.IsAppRelative(url)) return VirtualPathUtility.ToAbsolute(url);
return url;
}
catch (HttpException)
{
return url;
}
}
The problem is that it raises an exception if the provided URL contains a querystring, eg. ~/showcase/View.aspx?id=10783
.
Stack trace:
System.Web.HttpException was caught
Message='~/showcase/View.aspx?id=10783' is not a valid virtual path.
Source=System.Web
ErrorCode=-2147467259
StackTrace:
at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)
at System.Web.VirtualPathUtility.IsAppRelative(String virtualPath)
at Ctf.Common.ResolveUrl(String url) in [redacted]Common.cs:line 359
InnerException:
What's the problem here? Does VirtualPathUtility
just not support querystring URLs?
Edit: I should add that the helper is perfectly fine with other app-relative URLs as long as there isn't a querystring on them.
Edit2: I've made a hack workaround by removing the querystring from the URL before calling the VirtualPathUtility methods, then gluing it back on at the end. I'd rather avoid this if possible.