Saturday, March 26, 2011

how to find application root dynamically

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"% >' >

No comments:

ETG Consultancy

Web Designing Development Analysis & Promotion
Asp.Net 2.0 SQL WWF WCF SEO Marketing Ajax JQuery NHibernate MVC