本文转自: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 here 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 really depends on how often you need to use it... if this is a single use deal then just put it in the class where you 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 Statics
and I put functions like the above there...
just make sure you change the above from public string GetBaseUrl()
to public static string GetBaseUrl()
[转]How can I get my webapp's base URL in ASP.NET MVC,布布扣,bubuko.com
[转]How can I get my webapp's base URL in ASP.NET MVC
原文:http://www.cnblogs.com/freeliver54/p/3842416.html