【发布时间】: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