Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Custom errors on ASP.NET

Christian Horsdal edited this page Sep 22, 2015 · 3 revisions

I wanted to be able to return custom errors from a Nancy (0.9) app hosted in ASP.NET. Turns out that's easier said than done.

Get ASP and IIS out of your way

Add to or change your Web.config:

<configuration>
  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="PassThrough" />
  </system.webServer>
</configuration>

With this change, you should now see the default Nancy error pages. If that's all you want, excellent, but I wanted to return error messages straight from a route handler, so on we go!

Handy response extension

Pop this extension method somewhere:

public static Response AsError(this IResponseFormatter formatter, HttpStatusCode statusCode, string message)
{
    return new Response
        {
            StatusCode = statusCode,
            ContentType = "text/plain",
            Contents = stream => (new StreamWriter(stream) { AutoFlush = true }).Write(message)
        };
}

Now in your route handlers you can simply

Get["/"] = parameters => Response.AsError(HttpStatusCode.NotFound, "all gone");

But you'll still see the default Nancy error page! One more thing...

Have Nancy step aside

In your bootstrapper, you'll need to override the default error handler with your own. Here's what I did:

protected override NancyInternalConfiguration InternalConfiguration
{
    get
    {
        return NancyInternalConfiguration.WithOverrides(
            builder => builder.StatusCodeHandlers = new List<Type>());
    }
}

Then implement a NullErrorHandler class that inherits from IErrorHandler and handles all status codes by doing exactly nothing.

Now you'll see

% curl http://localhost/
all gone
Clone this wiki locally