Custom errors on ASP.NET - NancyFx/Nancy GitHub Wiki
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.
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!
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...
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