获取HTTP状态码的描述

发布于 2024-09-15 11:40:24 字数 144 浏览 3 评论 0原文

在 ASP.NET 中,您可以将 Response.StatusCode 设置为例如 404。是否应该始终设置状态行/描述? (在本例中为“404 页面未找到”)

如果您只有代码 (404),如何获得描述?这是框架中的某个地方还是您必须手动对描述进行硬编码?

In ASP.NET you can set the Response.StatusCode to for example 404. Should the status line / description always be set? (to in this case "404 Page Not Found")

How do you get the description if you only have the code (404)? Is this somewhere in the framework or do you manually have to hardcode the descriptions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尛丟丟 2024-09-22 11:40:25

您可以使用静态方法 HttpWorkerRequest.GetStatusDescription< /code>为此。

You can use the static method HttpWorkerRequest.GetStatusDescription for this.

余厌 2024-09-22 11:40:25

如果您在拉取 Response.StatusCode 的同时需要它,您可以从 Response.StatusDescription

If you need it at the same time you're pulling Response.StatusCode, you can get the description from Response.StatusDescription.

哆兒滾 2024-09-22 11:40:25

状态描述可以通过一些疯狂的类型转换来检索。
这是检索自定义异常消息的代码片段(这只是客户端代码)

try
{
    string exText = ((HttpWebResponse)w.Response).StatusDescription);
}
catch (WebException w)
{    
}

The status description can be retrieved with some crazy type casting.
Here is the code snipped which retrieves the custom exception message (this is client side code only)

try
{
    string exText = ((HttpWebResponse)w.Response).StatusDescription);
}
catch (WebException w)
{    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文