Reputation: 172
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
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
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
Reputation: 991
Try this:
HttpContext.Current.Request.UserHostName;
or
HttpContext.Current.Request.UserHostAddress;
Don't forget to add using System.Web;
.
Upvotes: 0
Reputation: 511
I believe you'll need the referring URL for that.
Request.UrlReferrer
Upvotes: 1