简体   繁体   中英

How to get the request URI of a RESTful webservice

I have a webservice that is used under several different websites and want to know which of the sites the request came through (and return different things depending on that data). How do I do this?

I have a website, example.com, that has one webservice in /webservice.svc . When the client comes to this site via http://client1.example.com/webservice.svc/hello I want to say "hello, client1!" and when they come through http://client2.example.com/webservice.svc/hello it should say "hello, client2!".

So client1 or client2 depending on subhost (or application directory)

Assuming you are using WCF, you can try: System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.Headers["Referer"]

The result will in your example be http://client1.example.com/webservice.svc/hello or http://client2.example.com/webservice.svc/hello . You can then parse that string for presence of client1 or client2.

You may want to check for nulls.

Ended up getting this done with HttpContext.Current.Request.Url.ToString();

It too returned "Object reference not set to an instance of an object" originally when I tried, but found that setting [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] before my class declaration as well as in web.config did the trick and allowed access to the variables I wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM