简体   繁体   中英

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.

Example:

https://xxx.test.com/test.html page is calling https://api.test.com/api/home/get/1 WEB method.

The requested url https://xxx.test.com/test.html need to read from the web method.

The below code is returning IP Address. It is not returning domain 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 . Note that this data was set by the client, so you can't trust it's 100% accurate.

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. In that case, look to HttpContext.Current.Request.Headers["X-Forwarded-For"] for the browser's IP address. Note that if you aren't behind such hardware, this is a great attack vector.

You can try HttpRequest.UrlReferrer . The MSDN reference can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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