What is the best way to override the default error handling for the 404 status code? By default, when you hit a 404 not found error from the browser it returns some plain text that starts with “Handler for Request not found:”. Instead, I would like it to be processed the same way as other exceptions by displaying the ServiceStack html template with the details of the exception, because this allows the custom error message to be displayed (i.e. Customer not found).
see: https://github.com/ServiceStack/ServiceStack/wiki/Error-Handling#customize-handling-of-c-exceptions
Matt Middleton-White:
Hi mate, yeah we have been trying to follow that but it looks like this ‘CustomHttpHandlers’ property doesn’t exist on HostConfig:
SetConfig(new HostConfig {
CustomHttpHandlers = {
{ HttpStatusCode.NotFound, new RazorHandler("/notfound") },
{ HttpStatusCode.Unauthorized, new RazorHandler("/login") },
}
});
We are using 4.0.11.0 from the Signed package. Are we missing something?
Ahh yes, all the handlers have moved to the AppHost in v4, I’ve now updated the docs to:
this.CustomErrorHttpHandlers[HttpStatusCode.NotFound] = new RazorHandler("/notfound");
Matt Middleton-White:
Great, thanks for that. The only thing left that I’m struggling with is how to access the default IServiceStackHandler that I can pass as the value to the CustomErrorHandlers dictionary. How can I get the default html handler that displays the ServiceStack html5 report?
What we are trying to achieve here is that a 404 gets handled the same way as any of the other exceptions that come out of the service; get the default page that shows the exception message.
ok the problem is that SS’s fallback error handlers for NotFound and Forbidden were built-in fallbacks and so couldn’t be unset to ‘null’ for it to be able to use the default error handling, I’ve just moved them into AppHost so they’re now overridable in this commit:
https://github.com/ServiceStack/ServiceStack/commit/c6a9b19118aa18459669e95b1d5b7c697732d367
Now you can remove the built-in handler by setting it to null:
this.CustomErrorHttpHandlers[HttpStatusCode.NotFound] = null;
so it will follow the default behavior of other errors.
This change has now been deployed to MyGet at:
https://github.com/ServiceStack/ServiceStack/wiki/MyGet
Matt Middleton-White:
Mate you are a legend. Works great, thanks.
Drazen Dotlic:
I have a somewhat related question: I’m seeing “Request Not Found” errors for the service which should be 100% correctly configured. I have checked web.config (so we’re talking IIS ASP.NET (not MVC) site) ServiceStack handler is being set correctly for a “locationPath” (so not hosted in root) and HostConfig’s HandlerFactoryPath has been set to the appropriate “same” value, what else could I have forgotten? It’s understood that I’m sending correct assemblies to the constructor of ServiceStackHost derived class.
If nothing obvious comes to mind, I will have to use the above mechanism to debug this (was at the point of doing it but then noticed like the original poster that the property CustomHttpHandlers disappeared) I’m just hoping someone might have a pointer.
Needless to say, I’ve checked forums concretely StackOverflow and all of the issues people have had were with strange/incorrect configuration that I’ve checked and that checks out.
+Drazen Dotlic can’t think of anything at the top of my head if the HandlerFactoryPath is correct, can you post the HTTP Request and Response headers (best to post as a new question or submit it to https://github.com/ServiceStack/Issues)
Drazen Dotlic:
+Demis Bellot As I developer myself, knowing how things are, I don’t want to spam GitHub issues with something rather exotic that’s not easy to repro and waste your time (you have other more important things to do, like implement some tiny new features we need ). I have 3 hosting “environments” (console, two web sites) of which only one doesn’t work yet all of the hosting code is shared in the same assembly so this cannot fail, but it does. I’ll see if I can repro outside of our product code (which I naturally cannot share) and then post an issue. Btw, HTTP headers look “normal” I can see ServiceStack’s X-Powered-By (something like that) header with SS value inside (strangely formatted as 4,011) so I know SS is handling the request, but all calls fail. Let’s give it a few days and I’ll revisit.