My NotFound error page is not returning a 404… What is the correct way to set your own 404 not found page? This is what I’m doing now and it’s returning a 200…
CustomErrorHttpHandlers.Add(HttpStatusCode.NotFound, new RazorHandler("/Errors/NotFound"));
Stephen Brannan:
Adding the following at the top of my NotFound.cshtml page…
base.Response.StatusCode = (int)HttpStatusCode.NotFound;
New Question though… Is there a way to change the returned content based on the content type? If the request was json then I’d like to return a json response otherwise return a html response?
Note: Howto questions requiring code like this are better suited for stackoverflow (it also helps build a knowledge-base). Just post a link to it in here.
If you return or throw a HttpError.NotFound() in your services it automatically returns a JSON error response, i.e the CustomErrorHttpHandlers is only used for HTML ContentTypes.
But if no service exists at all then it just uses your registered custom handler. The Service Clients (and most http clients) just see sthe 404 and throws a HTTP Error.
If you still wanted to return a customized error response you can add this in your Error Razor Page:
if (!Request.ResponseContentType.MatchesContentType(MimeTypes.Html))
{
Layout = “”;
Response.ContentType = Request.ResponseContentType;
var error = new ErrorResponse
{
ResponseStatus = DtoUtils.CreateResponseStatus(“NotFound”, “Not Found”)
};
HostContext.ContentTypes.SerializeToStream(base.Request, error, Response.OutputStream);
throw new StopExecutionException();
}
The other alternative is to register your own custom RazorHandler that detects and handles non HTML requests separately. Code for RazorHandler is at: https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Razor/RazorHandler.cs
Stephen Brannan:
+Demis Bellot Thanks for the help. I’ll note using stackoverflow for next time and apologize for posting it here. I had thought this was a brief question suitable for discussion.
Do you see value in a feature request for defining the content type with the custom error http handlers? For example I could have a custom NotFound RazerHandler for html and a custom NotFound service for json.
Thanks again +Demis Bellot When the holidays come around I hope you have an address where I can mail you something for all your hard work!
I don’t think there’s any value that justifies the added complexity, Error Responses from Services already have have pre-determined behavior. The only real error that’s left, is when no Services exist (i.e. a 404), this is equivalent to a fatal compile error where something is very wrong with the client if it’s trying to call a non-existing Service. Adding a JSON response in this case doesn’t add much value as HTTP Clients are just going to throw 404 errors anyway. You could also change the Razor page to emit no response for non HTML requests, since there’s no real added info you can return when you don’t know what the request was for.
Stephen Brannan:
You didn’t answer my question about how to send a gift to you during the holidays
oh, ok sure My snail mail address is: 1405 Colonial Gardens Drive, Avenel, NJ 07001, USA