简体   繁体   English

如何在ASP.NET Web API中获得客户端请求的URL?

[英]How to get the client requested url where the request is coming in the ASP.NET web api?

In My application the scenario like i want to read the client requested URL from ASP.NET WEB API. 在我的应用程序中,类似的场景我想从ASP.NET WEB API读取客户端请求的URL。

Example: 例:

https://xxx.test.com/test.html page is calling https://api.test.com/api/home/get/1 WEB method. https://xxx.test.com/test.html页面正在调用https://api.test.com/api/home/get/1 WEB方法。

The requested url https://xxx.test.com/test.html need to read from the web method. 要求的网址https://xxx.test.com/test.html需要从网络方法中读取。

The below code is returning IP Address. 下面的代码返回IP地址。 It is not returning domain url. 它不返回域URL。

// GET api/home/get/5
public string Get(int id)
{
return HttpContext.Current.Request.UserHostAddress;
}

Please suggest me. 请给我建议。

Thanks in Advance. 提前致谢。

Try this : 尝试这个 :

string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx

string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx

string host = HttpContext.Current.Request.Url.Host;
// localhost

Look to HttpContext.Current.Request.UrlReferrer.OriginalString . 查看HttpContext.Current.Request.UrlReferrer.OriginalString Note that this data was set by the client, so you can't trust it's 100% accurate. 请注意,此数据是由客户端设置的,因此您不能相信它是100%准确的。

If you're behind a load balancer or other reverse proxy, when asking for HttpContext.Current.Request.UserHostAddress , you won't get the IP of the client, instead you'll get the IP of the load balancer. 如果您位于负载平衡器或其他反向代理的后面,则在请求HttpContext.Current.Request.UserHostAddress ,您将不会获得客户端的IP,而会获得负载平衡器的IP。 In that case, look to HttpContext.Current.Request.Headers["X-Forwarded-For"] for the browser's IP address. 在这种情况下,请查看HttpContext.Current.Request.Headers["X-Forwarded-For"]作为浏览器的IP地址。 Note that if you aren't behind such hardware, this is a great attack vector. 请注意,如果您没有使用此类硬件,这是一个很好的攻击手段。

You can try HttpRequest.UrlReferrer . 您可以尝试HttpRequest.UrlReferrer The MSDN reference can be found here MSDN参考可以在这里找到

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

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