I would like to know how best to solve this problem.
The application has mvc web and web api. I have a separate mvc web controller (called Error) responsible for displaying error pages. My web.config looks like this:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<remove statusCode="500" />
<error statusCode="404" responseMode="ExecuteURL" path="/error/404" />
<error statusCode="500" responseMode="ExecuteURL" path="/error/500/" />
</httpErrors>
</system.webServer>
It works for mvc web pages, but for web api now, when I return NotFound (), I get html error pages in response. I decided to fix this using the location attribute:
<location path="api">
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<remove statusCode="500" />
</httpErrors>
</system.webServer>
</location>
So now it does not return html, but just an error string from asp.net.
But what if I need to return some object from the web api and just set the error header in action, for example, or just the error header without data?
thanks