简体   繁体   English

在Http Status Code 404上返回附加值的最佳方法

[英]Best way to return added value on Http Status Code 404

At times I return, at server level, some extra information alongside with a HTTP 404 有时我会在服务器级别返回一些额外的信息以及HTTP 404

For example, instead of returning just a 404 , which can puzzle my client whether the routing is correct or not, it will also receive something like 例如,与其返回404 ,不如让我的客户困惑路由是否正确,它还会收到类似

the identifier 'abc' is unknown 标识符“ abc”未知

I usually set the content type to text/plain and return some text in the Content 我通常将内容类型设置为text/plain然后在Content返回一些文本

Another alternative is to set the ReasonPhrase instead. 另一种选择是改为设置ReasonPhrase

Which one is the best way / convention? 最好的方式/惯例是哪一种? Set Content or set ReasonPhrase ? 设置Content或设置ReasonPhrase

The error message should be put in response body ( Content ), not in response Reason Phrase . 错误消息应放在响应正文( Content )中,而不是响应Reason Phrase

According to RFC 2616 根据RFC 2616

The Reason-Phrase is intended to give a short textual description of the Status-Code...The client is not required to examine or display the Reason-Phrase. 原因短语旨在提供状态代码的简短文字说明...不需要客户端检查或显示原因短语。

Some explanation: 一些解释:

  1. Reason-Phrase is short , textual description of Status-Code , it should describe Status Code itself, not custom error message. 原因短语是简短状态代码 文本描述,它应描述状态代码本身,而不是自定义错误消息。 If custom error message is very long, or the message has JSON structure, using Reason-Phrase certainly violates the specification. 如果自定义错误消息很长,或者消息具有JSON结构,则使用Reason-Phrase肯定会违反规范。
  2. As the specification indicate, the client (browser) is not required to examine the Reason-Phrase, which means Reason-Phrase may get ignored for some browsers, in some time. 如规范所示,客户端(浏览器)不需要检查原因短语,这意味着某些时候某些浏览器可能会忽略原因短语。

You can use custom error responses and overrides the 404 and any other error you want visit here Spring MVC: How to return custom 404 errorpages? 您可以使用自定义错误响应并覆盖404和您要在此处访问的任何其他错误Spring MVC:如何返回自定义404错误页面?

Create a view and set this code in app/Exception/Handler.php : 创建一个视图并在app / Exception / Handler.php中设置以下代码:

/*Render an exception into a response. / *将异常呈现给响应。 * * @param \\Illuminate\\Http\\Request $request * @param \\Exception $e * @return \\Illuminate\\Http\\Response * * @param \\ Illuminate \\ Http \\ Request $请求* @param \\ Exception $ e * @return \\ Illuminate \\ Http \\ Response

*/ * /

public function render($request, Exception $e) 公共函数render($ request,异常$ e)

{ {

if($e instanceof NotFoundHttpException)

{

    return response()->view('missing', [], 404);

}

return parent::render($request, $e);

} }

Set this use to get it working : 设置此用法以使其正常工作:

use Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException; 使用Symfony \\ Component \\ HttpKernel \\ Exception \\ NotFoundHttpException;

For more info you can visit 有关更多信息,您可以访问

https://httpd.apache.org/docs/2.4/custom-error.html https://httpd.apache.org/docs/2.4/custom-error.html

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/displaying-a-custom-error-page-cs https://docs.microsoft.com/zh-cn/aspnet/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/displaying-a-custom-error-page-cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM