在ASP.Net Core中获取HTTP状态代码说明

时间:2017-05-04 12:51:19

标签: asp.net .net-core

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

Get description for HTTP status code

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

5 个答案:

答案 0 :(得分:8)

这足以满足我的需求:

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

答案 1 :(得分:5)

您可以使用Microsoft.AspNetCore.WebUtilities.ReasonPhrases.GetReasonPhrase(int statusCode)

using Microsoft.AspNetCore.WebUtilities;

int statusCode = 404;
string reasonPhrase = ReasonPhrases.GetReasonPhrase(statusCode);

答案 2 :(得分:2)

在上一个答案的基础上,您可以用空格分隔HttpStatusCode枚举名称,例如:

public string GetStatusReason(int statusCode)
{
    var key = ((HttpStatusCode) statusCode).ToString();

    return string.Concat(
        key.Select((c, i) =>
            char.IsUpper(c) && i > 0
                ? " " + c.ToString()
                : c.ToString()
        )
    );
}

或者,如果您更喜欢正则表达式:

public string GetStatusReason(int statusCode)
{
    var key = ((HttpStatusCode) statusCode).ToString();
    return Regex.Replace(key, "(\\B[A-Z])", " $1");
}

您还可以将其简化为:

public string GetStatusReason(HttpStatusCode statusCode)
{
    var key = statusCode.ToString();
    return Regex.Replace(key, "(\\B[A-Z])", " $1");
}

看看枚举键的名称,它们看起来似乎很合理,所以大多数(即使不是全部)状态码也应产生可接受的原因消息。

答案 3 :(得分:2)

这是一个简单的实现,而不是使用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 "";
        }

答案 4 :(得分:0)

你可以做到

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