Click here to Skip to main content
15,397,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to know the URL (at least the server name) of an HttpRequest in my application.

www.request.com sends a request into my service (www.mywebserver.com).

Unfortunately, the Microsoft docs are less than adequate in explaining what the properties are for the HttpRequest.

Case in point:
HttpRequest Class (Microsoft.AspNetCore.Http) | Microsoft Docs[^]
PathBase - Gets or sets the RequestPathBase.

Clicking on PathBase yields this tidbit of information:
Gets or sets the RequestPathBase.

Paraphrasing here: It's of type PathString, and it's the RequestPathBase.

It's circular if you don't know the terminology. Is that MY pathbase (where the request is going to) or the pathbase of the calling webserver (where the request came from).

Not helpful in the least.

I'd be happy with a specific answer to my question (how to get the URL of the calling website) but I'd be THRILLED with an answer that could point me to something that actually what all of the methods/properties/extension methods are and what they do.

Thank you,
Bryan Clauss

What I have tried:

Scour the internet looking for examples that actually explain the HttpRequest object instead of examples that assume you already know what everything means.
Posted
Updated 13-Aug-20 0:30am
Comments
Sandeep Mewara 12-Aug-20 16:29pm
   
Have you tried:
1. Microsoft.AspNetCore.Http.Extensions.UriHelper.GetFullUrl(Request) OR
2.
public static class HttpRequestExtensions{    
        public static string GetRawTarget(this HttpRequest request)
        {    
           var httpRequestFeature = request.HttpContext.Features.Get<IHttpRequestFeature>();    
           return httpRequestFeature.RawTarget;
        }
}
F-ES Sitecore 13-Aug-20 7:03am
   
The header you're looking for is Referrer, as indicated by Richard in Solution 1, however this is generally only populated when someone navigates via a browser. If the call is one made programatically such as an API call then the referrer will only be filled in if the calling code supplies it, which it probably doesn't. If the referrer header doesn't contain the data you need then there is no way to get that data as anything can call your url, it doesn't have to be a website on a public domain.

1 solution

If you're looking for the details of the site which made the request, you need to look at the "Referer" header. But bear in mind that this may not be set at all, and the value may be entirely under the control of the calling code.
C#
Uri callingUrl = Request.GetTypedHeaders().Referer;
HeaderDictionaryTypeExtensions.GetTypedHeaders Method (Microsoft.AspNetCore.Http) | Microsoft Docs[^]
RequestHeaders.Referer Property (Microsoft.AspNetCore.Http.Headers) | Microsoft Docs[^]
Referer - HTTP | MDN[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900