Resolving URL in ASP.net using Tilda (~)

I have been using .net since it has come out and I just found this out (so, I am kind of pissed). When I wanted to link to another url in my site e.g. <a href=”~/folder/file.aspx”>click here</a> it never worked because the <a> tag doesn’t understand the tilde (~). The way I used to fix this is by adding the runat=server to the <a> tag which enables it to understand what it means.

I just found a better solution – using ResolveClientUrl and it looks like this:

<a
href=’<%= ResolveClientUrl(“~/folder/file.aspx”) %>
/>

Advertisements

0 thoughts on “Resolving URL in ASP.net using Tilda (~)

  1. Pingback: links for 2008-09-12 « dstelow notes…

  2. I was looking into why an example code I was using also used the tilda. Normally I try to expelectily write out file/link paths and was pretty curious as to why they were using it for their user controls.It turns out with .net server side controls when the app is compiled the tilda will be “translated” into a Request.ApplicationPath request from within the application project.This is why it wouldn't work without the runat=”server”. Its because it only works on the server side and is translated correctly on compulation.Hope this helps, someone else out if their trying to figure out why exactly this works :).

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s