This article transferred from: HTTP://STACKOVERFLOW.COM/QUESTIONS/1288046/HOW-CAN-I-GET-MY-WEBAPPS-BASE-URL-IN-ASP-NET-MVC
Maybe it is extension or modification of the answers posted this but I use simply following and it works:
Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~")
When my path is:http://host/iis_foldername/controller/action
Then I receive:http://host/iis_foldername/
public string GetBaseUrl()
{
var request = HttpContext.Current.Request;
var appUrl = HttpRuntime.AppDomainAppVirtualPath;
if(!string.IsNullOrWhiteSpace(appUrl)) appUrl += "/";
var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);
return baseUrl;
}
That's really depends on what often you need to use it ... if this is a, single, deal then just put it in the class where yo U need this data,
If you anticipate using it in multiple classes in your app, then I use a folder called Helpers
in the base of my app,
I have a static
class called and Statics
I put functions like the above there ...
Just make sure-the above from to public string GetBaseUrl()
public static string GetBaseUrl()