I want to get website url into my webapi method. e.g 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.
Asked
Active
Viewed 1.4k times
4
-
do you want the IP address of client calling you API? – Syed Ali Taqi Apr 13 '17 at 08:05
-
@SyedAliTaqi i want domain name of the client. – farrukh aziz Apr 14 '17 at 05:50
-
try [this](http://stackoverflow.com/a/1445109/3621001) – Syed Ali Taqi Apr 14 '17 at 05:59
4 Answers
5
You get some complete information along with port on which the api is hosted. But you will need to add System.Web to make it work.
HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd('/');

Himanshu Patel
- 754
- 8
- 13
1
I believe you'll need the referring URL for that.
Request.UrlReferrer

trevster344
- 491
- 2
- 14
-
-
Works for me. I make a request to my api from a jsfiddle demo. My urlReferrer comes back with: https://fiddle.jshell.net/_display/ What does your code look like? Sounds to me like you're accessing the property before making an api request. – trevster344 Apr 14 '17 at 14:47
0
Try this:
HttpContext.Current.Request.UserHostName;
or
HttpContext.Current.Request.UserHostAddress;
Don't forget to add using System.Web;
.

Anas Alweish
- 2,818
- 4
- 30
- 44

daylight
- 991
- 1
- 7
- 13
-
i have try this but it always gives me webapi hostaddress or hostname – farrukh aziz Apr 14 '17 at 05:49
0
If you're not using OWIN, then
HttpContext.Current.Request.Url.Host
That's in the System.Web
namespace
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:
Request.GetOwinContext().Request.Host.Value
The GetOwinContext extension function is provided by the Microsoft.AspNet.WebApi.Owin nuget package.

Duke Garland
- 58
- 7