farrukh aziz
farrukh aziz

Reputation: 172

Get host name in webapi method

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.

Upvotes: 4

Views: 17110

Answers (4)

Himanshu Patel
Himanshu Patel

Reputation: 764

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('/');

Upvotes: 5

Duke Garland
Duke Garland

Reputation: 68

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.

Upvotes: 0

daylight
daylight

Reputation: 991

Try this:

HttpContext.Current.Request.UserHostName;

or

HttpContext.Current.Request.UserHostAddress;

Don't forget to add using System.Web;.

Upvotes: 0

trevster344
trevster344

Reputation: 511

I believe you'll need the referring URL for that.

Request.UrlReferrer

Upvotes: 1

Related Questions