加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 是否可以使用自定义错误页面与MVC网站,但不是Web

发布时间:2020-12-15 18:38:22 所属栏目:asp.Net 来源:网络整理
导读:我有一个包含我的Web API控制器的/ api文件夹的MVC项目。我想要以下事情: 我的MVC网站在发生错误时提供自定义错误页面 我的Web API提供默认错误响应(json / xml包含异常和堆栈跟踪) 在我的MVC网站的web.config中,我有一个httpErrors节点,并将errorMode设
我有一个包含我的Web API控制器的/ api文件夹的MVC项目。我想要以下事情:

>我的MVC网站在发生错误时提供自定义错误页面
>我的Web API提供默认错误响应(json / xml包含异常和堆栈跟踪)

在我的MVC网站的web.config中,我有一个httpErrors节点,并将errorMode设置为“自定义”,以便在404s / 500s / etc时可以有漂亮的错误页面。在浏览MVC网站期间发生:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <remove statusCode="403" subStatusCode="-1" />
  <error prefixLanguageFilePath="" statusCode="404" path="Contentnotfound.htm" responseMode="File" />
  <error statusCode="500" path="/Errors" responseMode="ExecuteURL" />
  <error statusCode="403" path="/Errors/Http403" responseMode="ExecuteURL" /></httpErrors>

然而,使用该配置,当发生错误时,API将为自定义错误页面提供服务,而不是具有异常/堆栈跟踪(这是所需行为)的json / xml)。

有没有办法配置自定义错误,只适用于我的MVC网站而不是Web API?这个博客说没有(http://blog.kristandyson.com/2012/11/iis-httperrors-aspnet-web-api-fully.html),但是我想听听有没有人发现自从博客发表以来的工作。

我想如果我不能为我的Web API项目创建一个单独的项目/程序集。这将允许我分别为MVC和Web API配置httpErrors,但是我不想创建另一个项目,所以我还有另一个web.config进行配置。

解决方法

好吧,经过近一年的这个问题腌制,我再给他一个镜头。这是web.config魔术,让我想要的:
<!-- inside of <configuration> to allow error
    responses from requests to /api through -->
<location path="api">
    <system.webServer>
      <validation validateIntegratedModeConfiguration="false" />
      <httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough" >
        <clear/>
      </httpErrors>
    </system.webServer>
</location>

<!-- original httpErrors,inside of <location path="." inheritInChildApplications="false">
 to serve custom error pages when the MVC site returns an error code -->
<httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="400" subStatusCode="-1" />
      <remove statusCode="404" subStatusCode="-1" />
      <remove statusCode="500" subStatusCode="-1" />
      <remove statusCode="403" subStatusCode="-1" />
      <error statusCode="400" prefixLanguageFilePath="" path="Contentnotfound.htm" responseMode="File"/>
      <error prefixLanguageFilePath="" statusCode="404" path="Contentnotfound.htm" responseMode="File" />
      <error statusCode="500" path="/errors" responseMode="ExecuteURL" />
      <error statusCode="403" path="/errors/http403" responseMode="ExecuteURL" />
    </httpErrors>

这里发生的一个关键在于< location>节点允许您覆盖在较不具体路径上进行的设置。所以当我们对path =“。”的errorMode =“Custom”时,我们用< location path =“api”>来覆盖我们的Web API的路径。节点和其中的httpErrors配置。

以前我看过节点,但是我并没有明白这是他们到目前为止的目的。本文详细介绍了IIS / .NET的配置继承模型,我发现非常翔实:http://weblogs.asp.net/jongalloway/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读