16

I've to retrieve the orginal request url in my WCF rest webservice. Now my code looks like this:

public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
    protected override bool CheckAccessCore(OperationContext operationContext)
    {
        base.CheckAccessCore(operationContext);

        var url = operationContext.IncomingMessageProperties.Via.OriginalString;
        ...

web.config

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
      <standardEndpoints> 
         <webHttpEndpoint>
             <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

if my original url is

http://192.168.1.100:8081/test

this code return

http://hostname:8081/test

is there a way to retrieve the exact original request url?

Note

I found posts talking about cutomize "baseAddress" tag in web.config but I've no specific endpoint fom my extensionles webservice and I don't want to add it. I don't know if there is a way to do it without endpoint.

I found this post https://stackoverflow.com/a/5915713/735864 plays with System.Net.HttpRequestHeader.Host but with port number it doesn't works! I know I can parse provided url and do a Replace but... I don't think this is the best practice to achieve this.

Community
  • 1
  • 1
danyolgiax
  • 12,798
  • 10
  • 65
  • 116

2 Answers2

36
System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString;

This gives the original URI.

Karthik D V
  • 906
  • 1
  • 11
  • 19
  • 4
    One advantage of using WebOperationContext in contrast to HttpContext (referenced in another answer) is that it support a wider range of scenarios, including self-hosted services. HttpContext, on the other hand, is limited to aspNetCompatibilityMode and IIS hosting. – BitMask777 Oct 09 '13 at 20:23
  • 1
    Actually, RequestURI will always give you the FQDN. BaseURI will give you the domain name the browser actually submitted, which may not be the same. In REST scenarios, however, BaseURI does not include the rest path beyond the base service route. – Russell at ISC May 07 '14 at 13:23
  • 4
    I would like to use this, but `System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch` is `null` ... what could be the cause of this? – Hinek Nov 19 '14 at 13:11
  • 6
    Never mind ... `OperationContext.Current.RequestContext.RequestMessage.Headers.To` gives me what I need. – Hinek Nov 19 '14 at 13:28
  • karthik,@Hinek,everything that i tried here,return me servername.I mean url contain servername.it do not contain host url or ip – KumarHarsh Jan 27 '16 at 11:20
  • doesn't return the request url – fubo Aug 21 '17 at 14:00
  • When IIS starts running, I am getting the fqdn, site.domain.tld, but after running for a day or so, this suddenly switches to only the server hostname: Serv1. Nothing changed. No code changed. Site isn't under heavy load. – Rhyous Aug 07 '18 at 15:04
5

Short answer: var url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;

Long answer: See How to get the URL of the current page in C# .

Warning: This only works if you enable aspNetCompatibilityEnabled in web.config file.

Jalal
  • 6,594
  • 9
  • 63
  • 100
bhuber
  • 366
  • 3
  • 6
  • 8
    I think `HttpContext.Current` is only accessible for WCF services which are running in ASP.NET compatibility mode. – Josh M. Jan 30 '14 at 19:35
  • Yes, HttpContext.Current is null unless you run it ASP.NET compatibility mode, as is noted by @JoshM. – AlexVPerl Feb 01 '16 at 06:23