Get original request url in WCF REST service
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.
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, 2020Comments
-
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
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 over 10 yearsOne 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. over 10 yearsI think
HttpContext.Current
is only accessible for WCF services which are running in ASP.NET compatibility mode. -
Russell at ISC about 10 yearsActually, 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 over 9 yearsI would like to use this, but
System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch
isnull
... what could be the cause of this? -
Hinek over 9 yearsNever mind ...
OperationContext.Current.RequestContext.RequestMessage.Headers.To
gives me what I need. -
KumarHarsh over 8 yearskarthik,@Hinek,everything that i tried here,return me servername.I mean url contain servername.it do not contain host url or ip
-
AlexVPerl over 8 yearsYes, HttpContext.Current is null unless you run it ASP.NET compatibility mode, as is noted by @JoshM.
-
fubo over 6 yearsdoesn't return the request url
-
Rhyous almost 6 yearsWhen 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.