可以使用MVC网站的自定义错误页面,但不能使用Web API? [英] Is it possible to use custom error pages with MVC site but not Web API?

查看:161
本文介绍了可以使用MVC网站的自定义错误页面,但不能使用Web API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含我的Web API控制器的/ api文件夹的MVC项目。我想要以下内容:




  • 我的MVC网站在发生错误时提供自定义错误页面

  • 我的Web API提供默认错误响应(json / xml包含异常和堆栈跟踪)



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

 < httpErrors errorMode =CustomexistingResponse =Replace> 
< remove statusCode =404subStatusCode = - 1/>
< remove statusCode =500subStatusCode = - 1/>
< remove statusCode =403subStatusCode = - 1/>
< error prefixLanguageFilePath =statusCode =404path =Content\\\
otfound.htmresponseMode =File/>
< error statusCode =500path =/ ErrorsresponseMode =ExecuteURL/>
< error statusCode =403path =/ Errors / Http403responseMode =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魔术,让我想要的:

 <! - 在< configuration>允许错误
从/ api的请求到 - >的响应。
< location path =api>
< system.webServer>
< validation validateIntegratedModeConfiguration =false/>
< httpErrors errorMode =DetailedLocalOnlyexistingResponse =PassThrough>
< clear />
< / httpErrors>
< /system.webServer>
< / location>

<! - 原始httpErrors,位于< location path =。 inheritInChildApplications = 假 >
在MVC网站返回错误代码时提供自定义错误页面 - >
< httpErrors errorMode =自定义existingResponse =替换>
< remove statusCode =400subStatusCode = - 1/>
< remove statusCode =404subStatusCode = - 1/>
< remove statusCode =500subStatusCode = - 1/>
< remove statusCode =403subStatusCode = - 1/>
< error statusCode =400prefixLanguageFilePath =path =Content\\\
otfound.htmresponseMode =File/>
< error prefixLanguageFilePath =statusCode =404path =Content\\\
otfound.htmresponseMode =File/>
< error statusCode =500path =/ errorsresponseMode =ExecuteURL/>
< error statusCode =403path =/ errors / http403responseMode =ExecuteURL/>
< / httpErrors>

这里发生的症结在于< location> / code>节点允许您覆盖在较不具体路径上进行的设置。所以当我们为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


I have an MVC project with a /api folder which contains my Web API controllers. I want the following things:

  • My MVC site to serve a custom error page when an error occurs
  • My Web API to serve the default error response (json/xml containing exception and stack trace)

In the web.config for my MVC site, I have an httpErrors node, and have set the errorMode to "Custom" so that I can have nice error pages when 404s/500s/etc. occur during browsing of the MVC site:

<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="Content\notfound.htm" responseMode="File" />
  <error statusCode="500" path="/Errors" responseMode="ExecuteURL" />
  <error statusCode="403" path="/Errors/Http403" responseMode="ExecuteURL" /></httpErrors>

With that configuration however, the API will serve the custom error page when an error occurs, not json/xml with the exception/stack trace (which is the desired behavior).

Is there a way to configure custom errors to only apply to my MVC site and not the Web API? This blog says there is not (http://blog.kristandyson.com/2012/11/iis-httperrors-aspnet-web-api-fully.html), but I'd like to hear if anyone else has found a work around since that blog was published.

I suppose if there is not I could create a separate project/assembly for my Web API project. That would allow me to configure httpErrors for MVC and Web API separately, but I would prefer not to create another project just so I have yet another web.config to configure.

解决方案

Well, after a nearly a year of letting this question marinade, I gave it another shot. Here's the web.config magic that got me what I wanted:

<!-- 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="Content\notfound.htm" responseMode="File"/>
      <error prefixLanguageFilePath="" statusCode="404" path="Content\notfound.htm" responseMode="File" />
      <error statusCode="500" path="/errors" responseMode="ExecuteURL" />
      <error statusCode="403" path="/errors/http403" responseMode="ExecuteURL" />
    </httpErrors>

The crux of what's going on here is that the <location> node allows you to override settings made at a less specific path. So while we have errorMode="Custom" for path=".", we override that for the our Web API's path with the <location path="api"> node, and the httpErrors configuration within it.

I had seen nodes before, but it didn't dawn on me that this was their purpose until now. This article goes into more detail on the configuration inheritance model of IIS/.NET, which I found very informative: http://weblogs.asp.net/jongalloway/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides

这篇关于可以使用MVC网站的自定义错误页面,但不能使用Web API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆