Я выполнил свой собственный обработчик 404 для NancyFX, и он отлично работает, но есть проблема. Проблема в том, что он отменяет даже те запросы, в которых я хочу отправить код 404, но с моим пользовательским сообщением, например. "Пользователь не найден".NancyFX - Пользовательский обработчик 404 переопределяет каждый ответ 404
Handler
public class NotFoundHandler : IStatusCodeHandler
{
public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context)
{
if (statusCode == HttpStatusCode.NotFound)
{
// How to check here if the url actually exists?
// I don't want every 404 request to be the same
// I want to send custom 404 with Response.AsJson(object, HttpStatusCode.NotFound)
return true;
}
return false;
}
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
context.Response = new TextResponse(JsonConvert.SerializeObject(new { Message = "Resource not found" }, Formatting.Indented))
{
StatusCode = statusCode,
ContentType = "application/json"
};
}
}
Проблема
Get["/"] = _ =>
{
// This will not show "User not found", instead it will be overriden and it will show "Resource not found"
return Response.AsJson(new { Message = "User not found" }, HttpStatusCode.NotFound);
};