StatusCodes class and HttpStatusCode enum



为什么有System.Net.HttpStatusCode类和microt.aspnetcore . http . statuscodes enum,当他们似乎做同样的?

的例子:

public string Foo()
{
int statusCode = StatusCodes.Status409Conflict
// or
int statusCode = (int)HttpStatusCode.Conflict
return $"There is a {statusCode} here."
}

HttpStatusCodeEnum位于system.Net命名空间内,这意味着您可以在任何地方使用它,特别是当您需要从API读取信息时。

StatusCodes类在Microsoft.AspNetCore.Http名称空间

下一个是在使用中,StatusCodes类给你的代码作为int,但在HttpStatusCode你必须转换它们

使用它们来定位代码

不能将此添加为评论,因为我没有代表,但我在Asp.Net Core 6web应用程序中使用System.Net.HttpStatusCode来检查HttpResponse.StatusCode

System.Net默认存在,而Microsoft.AspNetCore.Http需要作为依赖项添加。

所以使用System.Net,如果你也有其他服务依赖于它(例如:IPEndPoint)。不需要单独安装。

你可以在这里找到更多信息

TLDR:这都是由于Asp.Net Core混乱的生态系统