我想在MVC应用程序中获取webapiconfig中的基本URL [英] I want to get base url in webapiconfig in MVC application

查看:399
本文介绍了我想在MVC应用程序中获取webapiconfig中的基本URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共字符串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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆