Is it possible to use custom error pages with MVC site but not Web API?

前端 未结 3 1287
旧时难觅i
旧时难觅i 2020-12-08 02:29

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 e
相关标签:
3条回答
  • 2020-12-08 03:00

    What have worked for me:

    <httpErrors existingResponse="Auto" defaultResponseMode="Redirect" errorMode="Custom">
      <remove statusCode="403" subStatusCode="-1" />
      <error statusCode="403" subStatusCode="-1" responseMode="Redirect" path="<!--path here-->" />
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" subStatusCode="-1" responseMode="Redirect" path="<!--path here-->" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" subStatusCode="-1" responseMode="Redirect" path="<!--path here-->" />
    </httpErrors>
    

    Seems like just setting existingResponse="Auto" will do the job.

    0 讨论(0)
  • 2020-12-08 03:04

    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

    0 讨论(0)
  • 2020-12-08 03:08

    I have not tested this, but how about writing the code to handle your MVC application errors, like shown here http://thirteendaysaweek.com/2012/09/25/custom-error-page-with-asp-net-mvc-4/ ?

    His code show that he is doing this at the application level (Global.asax), but I guess you could just as well trap exceptions at a lower level in the same way, with one method for MVC and another one for Web API.

    0 讨论(0)
提交回复
热议问题