禁用特定路径的“httpErrors”元素

Ami*_*adi 3 xml asp.net error-handling asp.net-mvc web-config

我在使用 Web.config 中的元素时遇到问题httpErrors

我决定使用 httpErrors 元素来处理 http 错误并在我的 (ASP.NET MVC) 应用程序中显示自定义错误页面。问题是我还有一个 API,我不希望 httpErrors 元素处理其错误,它有自己的自定义错误响应。

我希望在 API 方面禁用它。

我可以做些什么来实现我想要的吗?

Ami*_*adi 5

我找到了解决方案。

我们可以使用以下location元素来解决此类问题:

我们只需将以下代码放入configurationWeb.config 的元素(根)中:

  <location path="api">
    <system.webServer>
      <httpErrors existingResponse="PassThrough"></httpErrors>
    </system.webServer>
  </location>
Run Code Online (Sandbox Code Playgroud)

<location>元素实际上的作用是覆盖以您在路径属性中放入的内容开头的路径配置,在我的例子中是“api”。然后你要做的就是禁用,这可以通过将属性设置为 来httpErrors完成。existingResponsePassThrough

我希望这有帮助。