您的位置:首页 > 移动开发 > WebAPP

[转]How can I get my webapp's base URL in ASP.NET MVC

2014-07-14 11:50 495 查看
本文转自: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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: