Get original request url in WCF REST service

38,523

Solution 1

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

This gives the original URI.

Solution 2

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.

Share:
38,523
danyolgiax
Author by

danyolgiax

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook, The Wizardry Compiled

Updated on September 04, 2020

Comments

  • danyolgiax
    danyolgiax over 3 years

    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.

  • BitMask777
    BitMask777 over 10 years
    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.
  • Josh M.
    Josh M. over 10 years
    I think HttpContext.Current is only accessible for WCF services which are running in ASP.NET compatibility mode.
  • Russell at ISC
    Russell at ISC about 10 years
    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.
  • Hinek
    Hinek over 9 years
    I would like to use this, but System.ServiceModel.Web.WebOperationContext.Current.Incoming‌​Request.UriTemplateM‌​atch is null ... what could be the cause of this?
  • Hinek
    Hinek over 9 years
    Never mind ... OperationContext.Current.RequestContext.RequestMessage.Heade‌​rs.To gives me what I need.
  • KumarHarsh
    KumarHarsh over 8 years
    karthik,@Hinek,everything that i tried here,return me servername.I mean url contain servername.it do not contain host url or ip
  • AlexVPerl
    AlexVPerl over 8 years
    Yes, HttpContext.Current is null unless you run it ASP.NET compatibility mode, as is noted by @JoshM.
  • fubo
    fubo over 6 years
    doesn't return the request url
  • Rhyous
    Rhyous almost 6 years
    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.