Following function is help you to get root path or site base URL of your web application.
Like if your page URL like "http://www.blogger.com/post-create.g?blogID=5406317769839137062"
this function return base URL like "http://www.blogger.com".
public static string GetApplicationPath()
{
string appPath = null;
//Getting the current context of HTTP request
HttpContext context = HttpContext.Current;
//Checking the current context content
if (context != null)
{
//Formatting the fully qualified website url/name
appPath = string.Format("{0}://{1}{2}{3}",
context.Request.Url.Scheme,
context.Request.Url.Host,
context.Request.Url.Port == 80 ? string.Empty : ":" + context.Request.Url.Port,
context.Request.ApplicationPath);
}
return appPath;
}
Better One
public static string GetApplicationPath()
{
var request = HttpContext.Current.Request;
var appUrl = HttpRuntime.AppDomainAppVirtualPath;
if (!string.IsNullOrWhiteSpace(appUrl) && appUrl != "/")
appUrl += "/";
var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);
return baseUrl;
}
Like if your page URL like "http://www.blogger.com/post-create.g?blogID=5406317769839137062"
this function return base URL like "http://www.blogger.com".
public static string GetApplicationPath()
{
string appPath = null;
//Getting the current context of HTTP request
HttpContext context = HttpContext.Current;
//Checking the current context content
if (context != null)
{
//Formatting the fully qualified website url/name
appPath = string.Format("{0}://{1}{2}{3}",
context.Request.Url.Scheme,
context.Request.Url.Host,
context.Request.Url.Port == 80 ? string.Empty : ":" + context.Request.Url.Port,
context.Request.ApplicationPath);
}
return appPath;
}
Better One
public static string GetApplicationPath()
{
var request = HttpContext.Current.Request;
var appUrl = HttpRuntime.AppDomainAppVirtualPath;
if (!string.IsNullOrWhiteSpace(appUrl) && appUrl != "/")
appUrl += "/";
var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);
return baseUrl;
}