是否有可能使用与MVC网站的自定义错误页,但没有网络API? [英] Is it possible to use custom error pages with MVC site but not Web API?
问题描述
我有包含我的Web API控制器A / API文件夹中的MVC项目。我想下面的事情:
I have an MVC project with a /api folder which contains my Web API controllers. I want the following things:
- 我的MVC网站发生错误时服务自定义错误页
- 我的Web API服务默认的错误响应(包含异常,堆栈跟踪JSON / XML)
在web.config我的MVC网站,我有一个httpErrors节点,并设置为errorMode自定义,这样我可以有不错的错误页面时,404 / 500S /等。 MVC的网站在浏览网页时出现:
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>
使用该配置然而,API将发生错误时,除/堆栈跟踪(这是所需的行为)不JSON / XML服务自定义错误页。
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).
有没有配置自定义错误只适用于我的MVC网站的一种方式,而不是在Web API?该博客说没有(<一个href=\"http://blog.kristandyson.com/2012/11/iis-httperrors-aspnet-web-api-fully.html\">http://blog.kristandyson.com/2012/11/iis-httperrors-aspnet-web-api-fully.html),但我想听到的话,任何人发现周围的工作,因为该博客发表。
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.
我想,如果没有,我可以创造我的Web API项目一个单独的项目/组件。这将允许我单独配置的MVC httpErrors和Web API,但我会preFER不是创造另一个项目只是让我有另一种的web.config配置。
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.
推荐答案
好了,近一年让这个问题卤汁后,我给它一个镜头。这里的web.config中魔术这让我我想要的东西:
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>
什么是怎么回事的症结是,&LT;地点&gt;
节点,可以覆盖一个不太具体路径的设置。 。所以,当我们有errorMode =自定义为路径=,我们覆盖了我们的Web API的使用&LT路径,位置路径=API&GT;
节点,并在它的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.
我以前见过的节点,但我不明白,我说这是他们的目的,到现在为止。本文进入在IIS / .NET,我发现非常丰富的配置继承模型的更多细节:<一href=\"http://weblogs.asp.net/jongalloway/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides\">http://weblogs.asp.net/jongalloway/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides
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网站的自定义错误页,但没有网络API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!