Issues using HyperLink control with an image in a URL Rewriting scenario
Hey All,
I was using the HyperLink control in my page and added URL Rewriting to my page so that for example:
http://site/test/test/default.aspx would be rewritten to http://site/default.aspx. I had a HyperLink control on the default page like so:
<asp:HyperLink ID="hl1" runat="server" NavigateUrl="~/test.aspx" ImageUrl="~/test.jpg" Text="A"></asp:HyperLink>
And I was getting the dreaded error: Cannot use a leading .. to exit above the top directory...
Looking into this a little more if you remove the ImageURL property or even set it to be /test.jpg it will work fine, so conluded it must be something to be with the ImageUrl property in the control. Digging into reflector it looks like that when the HyperLink control renders it creates a new Image object and sets the ImageUrl of that to be ResolveClientUrl(url), internally the Image control also does a ResolveClientUrl again on that URL and I think this is where the issue is.
My solution was to create a FixUrl method in my utility class which would convert replace ~/ in a url with the current application base path hence giving me an absolute URL. This is also handy to use in your pages to set the urls of your CSS, JS etc.
Public Shared Function FixUrl(ByVal inURL As String) As String
Return If(inURL.StartsWith("~"), _
HttpContext.Current.Request.ApplicationPath & inURL.Substring(1), _
inURL).Replace("//", "/")
End Function
I would now assign my ImageUrl property in code i.e. img1.ImageUrl = FixURL("~/test.jpg") and this now works well.
If anyone has come past this before suggestions or ideas they would be appreciated.
Thanks
Stefan