简体   繁体   English

在 webapi 方法中获取主机名

[英]Get host name in webapi method

I want to get website url into my webapi method.我想将网站 url 放入我的 webapi 方法中。 eg my website name is abc.com and my api method url is xyz.com/getdetails?a=1 now on my webapi controller i want to get abc.com url to identify from this method was called.例如,我的网站名称是 abc.com,我的 api 方法 url 是 xyz.com/getdetails?a=1 现在在我的 webapi 控制器上,我想获取 abc.com url 以从调用此方法中识别。

You get some complete information along with port on which the api is hosted.您将获得一些完整的信息以及托管 api 的端口。 But you will need to add System.Web to make it work.但是您需要添加 System.Web 才能使其工作。

HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd('/');

Try this:尝试这个:

HttpContext.Current.Request.UserHostName;

or或者

HttpContext.Current.Request.UserHostAddress;

Don't forget to add using System.Web;不要忘记添加using System.Web; . .

我相信你会需要引用 URL。

Request.UrlReferrer

If you're not using OWIN, then如果您不使用 OWIN,则

HttpContext.Current.Request.Url.Host

That's in the System.Web namespace这是在System.Web命名空间中

If you're using OWIN and you're not using the Microsoft.Owin.Host.SystemWeb nuget package that provides classic asp.net HttpContext , you can also get it from OWIN context like this:如果您使用的是 OWIN 并且您没有使用提供经典 asp.net HttpContextMicrosoft.Owin.Host.SystemWeb nuget 包,您还可以从 OWIN 上下文中获取它,如下所示:

Request.GetOwinContext().Request.Host.Value

The GetOwinContext extension function is provided by the Microsoft.AspNet.WebApi.Owin nuget package. GetOwinContext 扩展函数由 Microsoft.AspNet.WebApi.Owin nuget 包提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM