获取ASP.Net核心中的HTTP状态代码说明

获取ASP.Net核心中的HTTP状态代码说明,asp.net,.net-core,Asp.net,.net Core,在以前的ASP.Net版本中,我们可以通过以下几种方式检索HTTP状态代码的描述: ASP.Net Core中是否有类似于HttpWorkerRequest.GetStatusDescription的内容?这已经足够满足我的需要了: var statusCode = httpContext.Response.StatusCode var description = ((HttpStatusCode)statusCode).ToString(); // 404 -> "NotFound"

在以前的ASP.Net版本中,我们可以通过以下几种方式检索HTTP状态代码的描述:


ASP.Net Core中是否有类似于HttpWorkerRequest.GetStatusDescription的内容?

这已经足够满足我的需要了:

var statusCode = httpContext.Response.StatusCode
var description = ((HttpStatusCode)statusCode).ToString(); // 404 -> "NotFound"

改进之前的答案,您可以使用空格拆分
HttpStatusCode
enum name,例如:

公共字符串GetStatusReason(int statusCode)
{
var key=((HttpStatusCode)statusCode).ToString();
返回字符串.Concat(
键。选择((c,i)=>
字符IsUpper(c)和&i>0
?“”+c.ToString()
:c.ToString()
)
);
}
或者,如果您更喜欢正则表达式:

公共字符串GetStatusReason(int statusCode)
{
var key=((HttpStatusCode)statusCode).ToString();
返回Regex.Replace(键“(\\B[A-Z])”,“$1”);
}
您还可以将其简化为:

公共字符串GetStatusReason(HttpStatusCode statusCode)
{
var key=statusCode.ToString();
返回Regex.Replace(键“(\\B[A-Z])”,“$1”);
}
查看枚举键名称,它们的名称似乎非常合理,因此大多数(如果不是全部的话)状态代码应该会产生可接受的原因消息。

您可以使用(如果在项目中尚未被Microsoft.AspNetCore.App等包短暂引用,则可以从中获取):

使用Microsoft.AspNetCore.WebUtilities;
int statusCode=404;
字符串reasonPhrase=ReasonPhrases.GetReasonPhrase(状态代码);

下面是一个简单的实现,它不使用NuGet或拼接字符串。这些描述应与HttpWorkerRequest的源匹配

        /// <summary>
        /// Descriptions for Http Status Code
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetHttpStatusDescription(int code)
        {
            switch (code)
            {
                case 100: return "Continue";
                case 101: return "Switching Protocols";
                case 102: return "Processing";
                case 200: return "OK";
                case 201: return "Created";
                case 202: return "Accepted";
                case 203: return "Non-Authoritative Information";
                case 204: return "No Content";
                case 205: return "Reset Content";
                case 206: return "Partial Content";
                case 207: return "Multi-Status";
                case 300: return "Multiple Choices";
                case 301: return "Moved Permanently";
                case 302: return "Found";
                case 303: return "See Other";
                case 304: return "Not Modified";
                case 305: return "Use Proxy";
                case 307: return "Temporary Redirect";
                case 400: return "Bad Request";
                case 401: return "Unauthorized";
                case 402: return "Payment Required";
                case 403: return "Forbidden";
                case 404: return "Not Found";
                case 405: return "Method Not Allowed";
                case 406: return "Not Acceptable";
                case 407: return "Proxy Authentication Required";
                case 408: return "Request Timeout";
                case 409: return "Conflict";
                case 410: return "Gone";
                case 411: return "Length Required";
                case 412: return "Precondition Failed";
                case 413: return "Request Entity Too Large";
                case 414: return "Request-Uri Too Long";
                case 415: return "Unsupported Media Type";
                case 416: return "Requested Range Not Satisfiable";
                case 417: return "Expectation Failed";
                case 422: return "Unprocessable Entity";
                case 423: return "Locked";
                case 424: return "Failed Dependency";
                case 500: return "Internal Server Error";
                case 501: return "Not Implemented";
                case 502: return "Bad Gateway";
                case 503: return "Service Unavailable";
                case 504: return "Gateway Timeout";
                case 505: return "Http Version Not Supported";
                case 507: return "Insufficient Storage";
            }
            return "";
        }
//
///Http状态代码的说明
/// 
/// 
/// 
公共静态字符串GetHttpStatusDescription(int代码)
{
开关(代码)
{
案例100:返回“继续”;
案例101:返回“交换协议”;
案例102:退货“处理”;
案例200:返回“OK”;
案例201:返回“已创建”;
案例202:返回“已接受”;
案例203:返回“非权威信息”;
案例204:返回“无内容”;
案例205:返回“重置内容”;
案例206:返回“部分内容”;
案例207:返回“多状态”;
案例300:返回“多选”;
案例301:返回“永久移动”;
案例302:返回“已找到”;
案例303:返回“见其他”;
案例304:返回“未修改”;
案例305:返回“使用代理”;
案例307:返回“临时重定向”;
案例400:返回“错误请求”;
案例401:返回“未经授权”;
案例402:返回“所需付款”;
案例403:返回“禁止”;
案例404:返回“未找到”;
案例405:返回“不允许使用方法”;
案例406:返回“不可接受”;
案例407:返回“需要代理身份验证”;
案例408:返回“请求超时”;
案例409:返回“冲突”;
案例410:返回“已消失”;
案例411:返回“所需长度”;
案例412:返回“前置条件失败”;
案例413:返回“请求实体太大”;
案例414:返回“请求Uri太长”;
案例415:返回“不支持的媒体类型”;
案例416:返回“请求的范围不可满足”;
案例417:返回“预期失败”;
案例422:返回“不可处理实体”;
案例423:返回“已锁定”;
案例424:返回“失败的依赖项”;
案例500:返回“内部服务器错误”;
案例501:返回“未执行”;
案例502:返回“坏网关”;
案例503:返回“服务不可用”;
案例504:返回“网关超时”;
案例505:返回“不支持Http版本”;
案例507:返回“存储不足”;
}
返回“”;
}
您可以这样做

int code = 404;
string statusText = Enum.GetName(typeof(HttpStatusCode), code);

不是真正的解决方案-您可以在ASP.Net中执行此操作,它不会给您提供用户友好的消息。@ChrisPeacock,这就是为什么我没有选择此作为答案的原因。如果有人发布了更好的帖子,那么我肯定会接受。我已经尝试从ASP.Net的HttpWorkerRequest复制代码。您是否有
HttpResponseMessage
?您似乎可以使用它的
ReasonPhrase
属性。它确实在内部使用了
HttpStatusDescription
,但遗憾的是类不是
public
reasonPhrase
的值是什么?在这种情况下,对于404,它将是
“找不到”
。对于
405
来说,这将是
“不允许使用方法”
等。可能需要指出的是,此方法在a中,并且不是Net Core本身的一部分。它是asp.Net 5.0的一部分,不需要Nuget。唯一的问题是状态代码200(OK),它被转换为
“OK”
(即O和K字符,中间有空格)。