[转]How can I get my webapp's base URL in ASP.NET MVC
本文转自: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()
posted on 2014-07-14 11:50 freeliver54 阅读(373) 评论(0) 收藏 举报
 
                    
                 
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号