Skip to content

how to find application root dynamically in asp.net

March 26, 2011

sometimes you might have experienced some of the images are not appearing on your web page because of inappropriate path. so you need to know the root of the application dynically, because that keep chaning from development server to testing server, testing server to production server.

so you just need to do the following.

public static string ApplicationRoot
{
get
{
string root = null;
if (HttpContext.Current.Request.ApplicationPath != “/”)
root = HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority) + “/” + HttpContext.Current.Request.ApplicationPath;
else
root = HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority) + “/”;

if (!root.EndsWith(“/”))
root += “/”;

return root;
}
}

now whenever you are adding some image in any usercontrol, and you want to make sure the image is appearing allover the places then just use the following code

< img src=’< %=ApplicationRoot +”Images/myImage.jpg”% >’ >

Advertisement
No comments yet

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.