marisks
marisks

Reputation: 1822

How to set status code 500 for error page response when exception occurred loading config file in ASP.NET

I have EPiServer application which sometimes might throw exception on application start when loading some configuration or exception occurs in EPiServer initialization pipeline. I have configured customErrors to redirect to /Error.htm page and I am handling this page's response status code in Application_EndRequest event in global.asax to return correct status code like this:

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        if (Request.Url.AbsolutePath.EndsWith("Error.htm"))
        {
            Response.StatusCode = 500;
            Response.TrySkipIisCustomErrors = true;
        }
    }

It works great when exceptions occur when application is loaded, but not when loading config file (episerver.config in my case) or exception occurs in EPiServer initialization pipeline (there is one bug in EPiServer).

Tried to create IHttpModule, but it is not initialized. Tried to add error handling in Application_Error, but it is not fired too.

It seems that ASP.NET is handling these exceptions because it redirects to my Error.htm page correctly, but it sets status code 304. And I cannot find a way to get into pipeline to change the status code.

I need 500 status code for error page to configure load balancer to take off misconfigured server.

UPDATE

I have set custom errors Off so that correct status code is sent to IIS:

<customErrors mode="Off" defaultRedirect="/Error.htm"></customErrors>

Configured httpError section to redirect to Error.htm, but full exception details are shown locally and remotely. When I add existingResponse="Replace" it throws exception because of loop of redirects:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="500" prefixLanguageFilePath="" path="Error.htm" responseMode="Redirect" />
</httpErrors>

Configured httpError section to ExecuteURL, but still full exception details are shown locally and remotely. When I add existingResponse="Replace" it still shows full exception details locally and remotely:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="500" prefixLanguageFilePath="" path="/Error.htm" responseMode="ExecuteURL" />
</httpErrors>

If I set responseMode="File" and existingResponse="Replace" it shows Error.htm locally and remotely:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="500" prefixLanguageFilePath="" path="Error.htm" responseMode="File" />
</httpErrors>

Still no luck to get error details locally and Error.htm remotely.

Upvotes: 2

Views: 4617

Answers (3)

cheesemacfly
cheesemacfly

Reputation: 11762

In EPiServer 7 and up you can use globalErrorHandling to set the expected behavior.

To get the error handling done by httpErrors/customErrors you can set the parameter to Off:

<episerver>
  <applicationSettings globalErrorHandling="Off" />
</episerver>

Possible values are "RemoteOnly|On|Off" as documented here: http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/8/Configuration/Configuring-episerver/

You can access this parameter in the back office as well (which is going to edit your web.config on save):

enter image description here

Upvotes: 2

Johan N
Johan N

Reputation: 21

You might need to disable the EPiServer error handling. You do that by opening your episerver.config and setting the "globalErrorHandling" attribute on the "" element to "Off".

With globalErrorHandling on, my customErrors and httpErrors settings would not work as expected.

Part of the answer was copied from this address: http://www.eyecatch.no/blog/2011/09/custom-error-pages-in-episerver/

Upvotes: 0

Ted Nyberg
Ted Nyberg

Reputation: 7391

You could probably look into <httpErrors> as opposed to <customErrors>?

Some info at http://tedgustaf.com/blog/2011/5/custom-404-and-error-pages-for-asp-net-and-static-files/.

Upvotes: 0

Related Questions