I would like to know how to better solve this problem.
The app has mvc web and web api registered. I have a separate mvc web controller (called Error) responsible for rendering error pages. My web.config looks like:
<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 return. I thought to fix this by using 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 doesn't return the html but just an error string from asp.net.
But what if I need to return some object from web api and just set error header in the action for example or just the error header without data?
Thanks
 
    
