WCF应用程序正在发送潜在危险请求的html响应,而不是错误异常

本文关键字:响应 异常 错误 html 请求 应用程序 WCF 危险 | 更新日期: 2023-09-27 18:23:57

我在global.asax中添加了一个全局错误处理程序,但无论我尝试什么,我似乎都无法阻止的IIS屏蔽响应

<!DOCTYPE html>
<html>
    <head>
        <title>A potentially dangerous Request.Path value was detected from the client (&amp;).</title>
...

我更希望它发送一条WebFaultException风格的消息

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">My custom error message here</string>

我该怎么做?

WCF应用程序正在发送潜在危险请求的html响应,而不是错误异常

我认为您遇到的是IIS中的默认行为,而不是WCF中的某些行为。IIS在请求到达WCF之前就已经接收到该请求,并发回警告。我认为您可以在web.config 中使用类似的东西来关闭IIS请求验证

<configuration>
   <system.web>
      <pages validateRequest="false" />
   </system.web>
</configuration>

或者这个:

<httpRuntime requestPathInvalidCharacters="" />

然后,一旦请求通过IIS,您就可以使用检查坏字符的自定义消息检查器在WCF中处理异常。

这里有一个链接到一个关于消息检查器的文档:

http://trycatch.me/adding-custom-message-headers-to-a-wcf-service-using-inspectors-behaviors/