about relative path in asp.net
Here is some text from a book that I read.
"One quirk that can catch unsuspecting developers is the way that master
pages handles relative paths. If all you're using is static text, this issue
won't affect you.. However, if you add <img> tag or any other HTML, tag that
points to another resource, problems can occur. The problem shows up if you
place the master page in a different directory from the content page that
uses it.. This is a recommended best practice for large websites. In fact
Microsoft encourages you to use a dedicated folder for storing all you
master pages. However if you're not suitably careful, this can cause
problems when you use relative paths. For example ,imagine you put a master
page in a subfolder named MasterPages and add the following <img> tag to the
master page: <img src="banner.jpg" /
Assuming the file \MasterPages\banner.jpg exists, this appears to work fins.
The image will even appear in the Visual Studio design environment. However,
if you create a content page in another subfolder, the image path is
interpreted relative to that folder. If the file doesn't exist there, you'll
get a broken link instead of your graphic. Even worse, you could conceivably
get the wrong graphic if another image has the same file name. This problem
occurs because the <img> tag is ordinary HTML. As a result ASP.NET won't
touch it. Unfortunately, when ASP.NET processes you content page, the
relative path in this tag is no longer appropriate. The same problem occurs
with <a> tags that provide links to other pages and with the <link> element
that you can use to connect the master page to a style sheet.
To solve you problem, you could try to think ahead and write you URL
relative to the content page where you want to use it.. But this creates
confusion and limits where you master page can be used. A better fix is to
turn you <img> tag into a server-side control, in which case ASP.NET will
fix the mistake:
<img src="banner.jpg" runat="server" />
This works because ASP:NEt uses this information to create an HTMLImage
server control. This object is created af ter the Page object for the master
page is instansiated. At this point ASP.NET interprets all the paths
relative to the location of the master page. And as with all server-side
control, you can further clear things up by using the ~/ character to create
a root-relative path. here's an example that clearly poins toa picture in an
Image folder in the root web application folder.
<img src="~/Images/banner.jpg" runat="server />"
Now to my question after this long text. Can somebody explain in different
way then the book why the relative path is according to the Content page if
you use a HTML tag and the relative path is according tho the master page if
you use a server-side control ?
I hope to get a better explanation than the book
|All times are GMT. The time now is 05:43 PM.|
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.