ardalis / SmartEnum

A base class for quickly and easily creating strongly typed enum replacements in C#.
MIT License
2.16k stars 169 forks source link

Not able convert the JSON value to SmartEnum from request Body in ASP.NET Core Web API application #115

Open sameerinfodb opened 3 years ago

sameerinfodb commented 3 years ago

I'm using SmartEnum in a Web API project wherein i have decorated one of the property of a model as shown below

[JsonConverter(typeof(SmartEnumValueConverter<RequestStatus, int>))] public RequestStatus Status { get; set; } = default!;

and i'm accepting the json in a HttpPost body i.e. FromBody attribute. But Asp.net Core is not able to convert the json in to RequestStatus Enum.

I'm sendint the request as below { "RequesterId": "309b272f-6369-4744-8bfd-f7b2b2024530", "requestDisplayID":"1", "requestDate":"2021-04-04T01:28:00.141Z", "applicationID":"1", "approverID":"1", "jobID":null, "workloadID":"1", "Status":1 }

Can you suggest what change need to be done so that Asp.net Core can convert Status:"1" to SmartEnum

I'm using Ardalis.SmartEnum 2.0.0 Ardalis.SmartEnum.JsonNet 1.0.3 Ardalis.SmartEnum.Utf8Json 1.0.3

jafin commented 3 years ago

@sameerinfodb Is your project setup to use Newtonsoft as the default deserializer?

i.e.

services.AddControllers().AddNewtonsoftJson();
services.AddControllersWithViews().AddNewtonsoftJson();
services.AddRazorPages().AddNewtonsoftJson();