0

我有一个在多个不同网站下使用的网络服务,并且想知道请求来自哪些网站(并根据该数据返回不同的内容)。我该怎么做呢?

我有一个网站 example.com,它在 /webservice.svc 中有一个 web 服务。当客户通过http://client1.example.com/webservice.svc/hello来到这个站点时,我想说“你好,client1!” 当他们通过http://client2.example.com/webservice.svc/hello它应该说“你好,client2!”。

所以client1或client2取决于子主机(或应用程序目录)

4

2 回答 2

1

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.

于 2013-04-08T15:49:19.077 回答
1

最后用 HttpContext.Current.Request.Url.ToString();

当我尝试时,它最初也返回“对象引用未设置为对象的实例”,但发现在我的类声明之前以及在 web.config 中设置 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 可以解决问题并允许访问我想要的变量。

于 2013-04-09T12:06:00.207 回答