Search code examples
asp.netxmlasp.net-mvcerror-handlingweb-config

Disable "httpErrors" element for a specific path


I'm having trouble with httpErrors element in Web.config.

I decided to use httpErrors element to handle http errors and show custom error pages in my (ASP.NET MVC) application. The problem is that I also have an API that I don't want httpErrors element to handle its errors, it has its own custom error responses.

I want it to be disabled when it comes to API.

Is there anything I can do to achieve what I want?


Solution

  • I found the solution.

    We can fix such problems by using the location element:

    We just have to put the code below in the configuration element (the root) of the Web.config:

      <location path="api">
        <system.webServer>
          <httpErrors existingResponse="PassThrough"></httpErrors>
        </system.webServer>
      </location>
    

    What the <location> element actually does is that it overrides the configurations for paths that start with what you put in the path attribute, which in my case is "api". Then what you have to do is disable the httpErrors, which can be done by setting the existingResponse attribute to PassThrough.

    I hope this helps.