我想在MVC应用程序中获取webapiconfig中的基本URL [英] I want to get base url in webapiconfig in MVC application
问题描述
公共字符串GetHostUrl()
{
返回string.Format(http {0}:// {1} { 2},
(HttpContext.Current.Request.IsSecureConnection)?s:,
HttpContext.Current.Request.Url.Host
);
}
我尝试过:
i希望在mvc应用程序中的WebApiConfig中获取基本URL,我的应用程序运行在'http:// www .abc.com','http:// localhost:45678'
等
public string GetHostUrl()
{
return string.Format("http{0}://{1}{2}",
(HttpContext.Current.Request.IsSecureConnection) ? "s" : "",
HttpContext.Current.Request.Url.Host
);
}
What I have tried:
Hi,
i want to get base url in WebApiConfig in mvc application, where my application is running like 'http://www.abc.com', 'http://localhost:45678'
etc
推荐答案
这样的事情:
Something like this:
public string GetHostUrl()
{
HttpContext context = HttpContext.Current;
if (context == null) throw new InvalidOperationException("HttpContext is not available.");
return context.Request.Url.GetLeftPart(UriPartial.Authority);
}
这篇关于我想在MVC应用程序中获取webapiconfig中的基本URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!