asp.net mvc - Is it possible to use custom error pages with MVC site but not Web API? -


i have mvc project /api folder contains web api controllers. want following things:

  • my mvc site serve custom error page when error occurs
  • my web api serve default error response (json/xml containing exception , stack trace)

in web.config mvc site, have httperrors node, , have set errormode "custom" can have nice error pages when 404s/500s/etc. occur during browsing of 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 configuration however, api serve custom error page when error occurs, not json/xml exception/stack trace (which desired behavior).

is there way configure custom errors apply mvc site , not web api? blog says there not (http://blog.kristandyson.com/2012/11/iis-httperrors-aspnet-web-api-fully.html), i'd hear if else has found work around since blog published.

i suppose if there not create separate project/assembly web api project. allow me configure httperrors mvc , web api separately, prefer not create project have yet web.config configure.

well, after year of letting question marinade, gave shot. here's web.config magic got me wanted:

<!-- inside of <configuration> allow error     responses requests /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">  serve custom error pages when mvc site returns 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 <location> node allows override settings made @ less specific path. while have errormode="custom" path=".", override our web api's path <location path="api"> node, , httperrors configuration within it.

i had seen nodes before, didn't dawn on me purpose until now. article goes more detail on configuration inheritance model of iis/.net, found informative: http://weblogs.asp.net/jongalloway/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -