【问题标题】:WCF RESTful Web Service under IIS - URL Rewriting (getting 404 errors)IIS 下的 WCF RESTful Web 服务 - URL 重写(出现 404 错误)
【发布时间】:2011-08-08 22:01:23
【问题描述】:

我有一个在 IIS 下运行的 WCF RESTful Web 服务(使用 webHttpBinding),通过一个 SVC 文件(在 http://projectserver/BlarghService/BlarghService.svc 访问)公开。

我可以使用 BlarghService.svc 文件通过每个服务上定义的 URI 模板访问服务端点,如下所示:

http://projectserver/BlarghService/BlarghService.svc/accounts/
http://projectserver/BlarghService/BlarghService.svc/accounts/342432

这两个都可以正常工作。

但是重要的是我从 URI 中删除了实现细节,所以我使用了 IIS URI 重写模块并设置了通配符重写规则,该规则将 /BlarghService/ 之后的所有内容添加到 BlarghService.svc 的末尾,因此将以下内容转换为早期的陈述:

http://projectserver/BlarghService/accounts/
http://projectserver/BlarghService/accounts/342432

但是,当我请求这些资源时,我收到 404 错误。这些不是 IIS 生成的 404 错误,而是由 ASP.NET 本身生成的错误(因此 IIS 正确地重写了 URL)。但是 ASP.NET 决定检查指定的文件(“BlarghService.svc/accounts/342432”)是否存在于文件系统中,它应该将其传递给我的 Web 服务。

但奇怪的是,当我将报告的“请求的 URL”(来自 ASP.NET 404 错误)复制回地址栏中时,它工作正常。

那么发生了什么?

编辑:这是我的 web.config(位于“BlarghService”目录中)

<system.web>
    <compilation debug="true" />
    <authentication mode="None" />
    <customErrors mode="Off" />
</system.web>

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <!-- Currently ASP.NET steps in says it's a 404 error when it should be handled by BlarghService.svc, I've no idea why. -->
    <rewrite>
        <rules>
            <rule name="BlarghServiceRewriteRule" patternSyntax="Wildcard">
                <match url="*" />
                <action type="Rewrite" url="BlarghService.svc/{R:0}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
        <baseAddressPrefixFilters>
            <add prefix="http://projectserver/BlarghService" />
        </baseAddressPrefixFilters>
    </serviceHostingEnvironment>
    <services>
        <service name="Foo.Blargh.Api.BlarghService" behaviorConfiguration="BlarghServiceBehavior">

            <endpoint name="BlarghEndpoint" address="BlarghService.svc" behaviorConfiguration="BlarghEndpointBehavior" binding="webHttpBinding" contract="RH.Blargh.Api.BlarghService" />

        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="BlarghServiceBehavior">
                <serviceMetadata httpGetEnabled="false" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="BlarghEndpointBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

【问题讨论】:

  • 检查以确保除了您的 wcf 调用之外,您没有使用试图提交的 aspx 按钮或表单。此外,如果您不使用 .ajax 和 jQuery 来调用 wcf 服务,我强烈建议您采用这种方法。
  • Web 服务中没有任何 ASP.NET 组件。从网络浏览器发出 GET 请求重写 URL 时出现错误。
  • 你能发布你的 url 重写匹配模式并重写 url 吗?
  • 我已经编辑了我的帖子以显示 web.config 的相关部分。

标签: wcf iis url-rewriting


【解决方案1】:

更新:事实证明,WCF 一次只能响应一个特定的 HTTP 主机标头,并且我的 URL 重写涉及响应某些主机标头而不是其他主机标头。通过对一组主机头进行标准化并相应地重新制定我的重写规则,它开始起作用了。

【讨论】:

  • Dai,好久不见,或许你还可以分享更多细节?您是否仍有固定的重写规则并可以共享它们?我遇到了同样的问题,很高兴了解如何解决它。
  • @Ivan 我的 webservice/IIS 网站绑定到“fooservice.com”和“fooservice.com” - 我决定删除“www.fooservice.com”的绑定,它工作正常“fooservice.com”绑定。
  • 谢谢,戴!我将挖掘 Wireshark 重写和直接 HTTP 请求之间的区别。
  • 找到了我的问题的根本原因 - IIS 正在使用 *.svc 文件的本地处理程序,必须在本地 Web.config 中删除它( )
猜你喜欢
  • 1970-01-01
  • 2013-10-29
  • 2018-01-24
  • 2017-02-07
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
相关资源
最近更新 更多