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

3 Comments

  • Looks interesting. With this solution is it possible to still have your query string variables? What I like about the way I am doing it is that you could effectively turn off URL masking and it would still work fine.


    Thanks

    Stefan

  • Ya, rather than masking, it serves the page at the requested url by loading the right handler :)

    In regards to query strings

    You can have query string variables and they work as normal.

    The example just looks for the language part and removes it to determinethe real page, it passes the query string along as-is

    The only time you should have any issue with the qs is if you want to use it to convert parts of the path into a query string.

    Its better to keep a custom dictionary (eg "PathParamers") in the httpcontext.current items to hold them.
    (If you convert the path directly to qs arguments, so that site/color/red/All.aspx executes the pages site/All.aspx?color=red, then when you have postback, you postback to site/color/red/All.aspx?color=red)

  • Thanks,

    Yeah that is my case http://site/test/param1/param2.aspx gets rewritten to http://site/test.aspx?param1=x&param2=x I know this could always be stored in a dictionary but I like the idea that if I swap out the url rewriting with my current solution it will still work.


    Thanks

    Stefan

Comments have been disabled for this content.