IIS下的WCF RESTful Web服务 - URL重写(获取404错误)

时间:2011-08-08 22:01:23

标签: wcf iis url-rewriting

我有一个在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>

1 个答案:

答案 0 :(得分:0)

更新:事实证明,WCF一次只能响应一个特定的HTTP主机头,并且我的URL重写涉及响应某些主机头但不涉及其他主机头。通过标准化一组主机头并相应地重新构建我的重写规则,它开始工作。