Why is Microsoft.AspNetCore.Http.StatusCodes not designed as an enum?
Asked Answered
W

2

8

To learn new things about software design, I often analyze .NET framework source codes.

During my study today the Microsoft.AspNetCore.Http.StatusCodes class drew my attention when I saw it is not implemented as an enum, but rather as a static class with public const int fields (not even properties).

Why would you not use an enum for this purpose when it seems like a perfect solution? What might be the thought process behind this design decision?

I think I might be missing something here and I would be more than happy to learn something from this experience.

Whomever answered 28/4, 2019 at 18:46 Comment(2)
There are some discussions on this in the aspnet GitHub repo: here and here. There's probably more too if you search hard enoughMartinsen
This discussion actually seems more directly related to your questionMartinsen
S
5

Here is an answer by Tratcher:

Status codes are not a closed set, any code between 100 and 999 is allowed. Enums work best for closed sets where all of the values are known. That enum does not even include all the standardized codes. It also has multiple entries for some codes. I've found the usability to be lacking as it obscures the actual status code from you.

Ref: https://github.com/aspnet/Mvc/issues/6997#issuecomment-388554278

Shipman answered 18/8, 2021 at 19:33 Comment(0)
C
1

You can use System.Net.HttpStatusCode enum instead. It accessible both in Core and classic framework

Countermove answered 23/11, 2020 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.