Help: root path reference

H

hb

Hi,

Recently I read some articles about the root path reference in ASP.Net
application.
One recommended way is to use "~" at the beginning of the path with server
control.
for example:
<img runat="server" src="~/images/logo1.jpg">

Those articles also say that such method works with virtual directory as
well.

My question is: Is there any performance setback to use server side HTML
tag (like <img runat="server">)or even ASP.Net server control (like
<asp:image>)
instead of regular HTML tag (like <img>) just for root reference purpose?

Thank you

Hongbo
 
K

Karl Seguin

Hongbo:
Yes, there is a performance setback, but it's very small and probably worth
paying it (we are talking microseconds most likely).

If this isn't acceptable, you can have a global class with a property such
as:

public sealed class Globals{
static public string ApplicationPath {
get {
string applicationPath = HttpContext.Current.Request.ApplicationPath;
if (applicationPath == "/") {
return string.Empty;
}
return applicationPath;
}
}
}
}

and then use src="<%=Globals.ApplicationPath%>images/logo1.jpg" but I my
personal opinion is that ~ is much nicer and cleaner with a very low cost.

Karl
 
H

hb

Karl,

Thank you every much.
Karl Seguin said:
Hongbo:
Yes, there is a performance setback, but it's very small and probably worth
paying it (we are talking microseconds most likely).

If this isn't acceptable, you can have a global class with a property such
as:

public sealed class Globals{
static public string ApplicationPath {
get {
string applicationPath = HttpContext.Current.Request.ApplicationPath;
if (applicationPath == "/") {
return string.Empty;
}
return applicationPath;
}
}
}
}

and then use src="<%=Globals.ApplicationPath%>images/logo1.jpg" but I my
personal opinion is that ~ is much nicer and cleaner with a very low cost.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top