views:

58

answers:

3

hi


1) With Html elements that don’t have runat = “server” attribute, absolute and relative paths are sent to the browser as they are. But with server controls, Asp.Net runtime provides URL rebasing feature, where URLs ( specified by server controls contained inside Master Pages, Themes or User Controls ) relative to Master pages, Themes or User Controls are dynamically modified so that browser can locate referenced resources using URLs relative to the final page sent to the browser.

a) Besides master pages, themes and user controls, are there any other situations where URL rebasing feature is needed?


2) Assuming test.aspx is located in the root of a web application ( WebApp20/test.aspx ), while image file is located at WebApp20/Images/award.gif, then in the final page sent to the browser both of the following URLs will resolve to src=”Images/award.gif”.

test.aspx:

<img id="A" src="Images/award.gif" alt="image" runat="server"  /> 

<br>

<img id="B" src= "~/Images/award.gif" alt="image” runat="server"  /> 


While I understand that “~” is resolved by Asp.Net to the root of current web application and I also see the benefits of using it in certain situations, in the example above control B doesn’t really need it to find an image, and thus B could instead have src="Images/award.gif". And yet I’ve seen quite a few code examples where programmers used "~/Images/award.gif" instead of "Images/award.gif".

a) So is there a particular reason why in the example above we should prefer using "~/Images/award.gif" instead of "Images/award.gif"?


thanx

+3  A: 

The tilda (~) is sort of shorthand in this case. Its used to express the root of your application. The tilda is read by the ResolveClientUrl and the ResolveUrl methods and they return the path based on the actual root (you can call these methods directly too - more information in the links below).

There are a couple of good reasons to use this method:

REASON 1

Your application's root when deployed to one site, might be '/' and on another site, it might be '/somefolder/'. Using this method will give you a correct path in both cases.

REASON 2

If you include this image tag in a user control or in your master page, then the resulting pages could have multiple paths (therefore, the relative path to the images folder could be different for each page). This will give you a way to get to the images folder wherever you are in your application.

REASON 3

During development, things change a lot. You start an application with one set of requirements, and the client changes their mind. You may start an application with one design, and see that you need to change things. In either of these cases, you may have to move pages around (into and out of folders). Using this method allows you to move files without having to rewrite them.

See: Control.ResolveUrl Method

See: Control.ResolveClientUrl Method

Gabriel McAdams
I see the necessity of using ~ in situations you've mentioned, but in I don't see any benefits of using ~ in the example I've provided
carewithl
What happens when you move your page from the root to some sub folder?
Gabriel McAdams
So I should use ~ just in case I decide to move a page in a future, even if at this moment I'm sure the page will stay where it is?
carewithl
Yes. It's always best to develop in a way that reduces work later. Think about it this way. It takes 2 seconds more to use the ~. How much more work would it take to find and change who knows how many images, links, etc IF things were to change later?
Gabriel McAdams
thank you all for your help
carewithl
+1  A: 

controls might be included from pages located in various paths. in that case, the control's html would need to specify the full path with ~ or another method, since it can't guarantee the relative path will be valid in all pages that include it.

jspcal
I see the necessity of using ~ in the situation you've mentioned, but I don't see any benefits of using ~ in the example I've provided
carewithl
yeah it's best for controls and templates, it could also help if you move the page during development... then again, the images or whatever directory is referenced could move, so sometimes a relative path is better.
jspcal
+1  A: 

URL re-writing is another example of where rebasing can be used (or should be avoided).

For example, if you call:

context.RewritePath(url, false);

That prevents URLs in the destination page from being rebased to the new URL, so that relative URLs in the page will still work correctly. Setting the second argument to true will cause the URLs on the page to be rebased.

RickNZ