Get HTTP status code descriptions in ASP.Net Core

In previous versions of ASP.Net, we could get a description of the HTTP status code in several ways, as shown below:

Get Description for HTTP Status Code

Is there something similar HttpWorkerRequest.GetStatusDescriptionin ASP.Net Core?

+8
source share
3 answers

You can use : Microsoft.AspNetCore.WebUtilities.ReasonPhrases.GetReasonPhrase(int statusCode)

using Microsoft.AspNetCore.WebUtilities;

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

This is close enough for my needs:

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

HttpStatusCode 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");
}

, , , , .

+2
source

Source: https://habr.com/ru/post/1016991/


All Articles