This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3045|Reply: 0

[Source] .NET/C# uses Refit to request an enum parameter enum type error

[Copy link]
Posted on 7/23/2023 3:13:05 PM | | | |
Requirements: If there is an enum type in the parameter, the interface will return a "400 Bad Request" error, because Refit converts the enum in the object to a string by default, and the interface cannot convert the string to an enum error, as follows:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-3aef79a91ce9c411883953c9aaccb48a-68917b547ba8a2e5-00",
    "errors": {
        "req": [
            "The req field is required."
        ],
        "$.languagePermissions": [
            "The JSON value could not be converted to LanguagePermissionsEnum. Path: $.languagePermissions | LineNumber: 0 | BytePositionInLine: 145."
        ]
    }
}



The official explanation is that the reason for converting enums to strings by default is becauseIncrease readability and facilitate reading of parameter content information。 Reference:The hyperlink login is visible.

The default behavior is to serialize the enum to a string, which you can change by providing the serializer with your own settings:The hyperlink login is visible.



So, we pass in the custom Refit settings and don't use the default settings, deleteJsonStringEnumConverterThe converter is fine, the code is as follows:

Solution:The hyperlink login is visible.
(End)




Previous:.NET/C# lightweight Mapster object mapping framework
Next:.NET/C# Simple Use Polly tutorial
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com