禁用特定路径的 "httpErrors" 元素

Disable "httpErrors" element for a specific path

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

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

我希望它在 API 时被禁用。

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

我找到了解决方案。

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

我们只需要将下面的代码放在 Web.config 的 configuration 元素(根)中:

  <location path="api">
    <system.webServer>
      <httpErrors existingResponse="PassThrough"></httpErrors>
    </system.webServer>
  </location>

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

来完成

希望对您有所帮助。