4

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.

farrukh aziz
  • 162
  • 1
  • 2
  • 9

4 Answers4

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
  • it always give null value. – farrukh aziz Apr 14 '17 at 05:53
  • 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
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.