Search code examples
.netasp.net-core

What's the difference between System.Net.HttpStatusCode and Microsoft.AspNetCore.Http.StatusCodes?


In ASP.NET core, there seem to be two types available for specifying HTTP status codes:

What's the difference between these types, and in which use cases should I use each type?

using System.Net;
using Microsoft.AspNetCore.Http;

public class MyClass
{
    public string GetStatusCodeMessage()
    {
        int statusCode_1 = StatusCodes.Status409Conflict
        int statusCode_2 = (int)HttpStatusCode.Conflict

        // Which variant should I use?
        return $"There is a {statusCode_1} here."
    }
}

Solution

  • The HttpStatusCode Enum is are inside the system.Net namespace, which means you can use it anywhere, especially when you need to read information from an API.

    StatusCodes Class are inside the Microsoft.AspNetCore.Http namespace

    The next one is in use, the StatusCodes class gives you the codes as an int ,But in HttpStatusCode you have to cast them

    Use them to position the code