【问题标题】:How to get the request URI of a RESTful webservice如何获取 RESTful Web 服务的请求 URI
【发布时间】:2023-03-23 13:13:01
【问题描述】:

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

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

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

【问题讨论】:

标签: c# web-services


【解决方案1】:

假设您使用的是 WCF,您可以尝试: System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.Headers["Referer"]

在您的示例中,结果将是 http://client1.example.com/webservice.svc/hellohttp://client2.example.com/webservice.svc/hello。然后,您可以解析该字符串是否存在 client1 或 client2。

您可能想要检查空值。

【讨论】:

  • 这给了我以下异常:对象引用未设置为对象的实例。
  • 调试并查看 null 的结果。
【解决方案2】:

最终使用 HttpContext.Current.Request.Url.ToString(); 完成了这项工作;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    相关资源
    最近更新 更多