在webapi方法中获取主机名

far*_*ziz 4 c# asp.net asp.net-web-api

我想将网站 url 添加到我的 webapi 方法中。例如,我的网站名称是 abc.com,我的 api 方法 url 是 xyz.com/getdetails?a=1 现在在我的 webapi 控制器上,我想获取 abc.com url 来识别该方法被调用。

Him*_*tel 5

您可以获得一些完整的信息以及托管 api 的端口。但您需要添加 System.Web 才能使其正常工作。

HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd('/');
Run Code Online (Sandbox Code Playgroud)