Search code examples
c#jsonasp.net-coreasp.net-core-mvcasp.net-core-3.0

ASP.NET Core 3.0 [FromBody] string content returns "The JSON value could not be converted to System.String."


Using [FromBody] string content on an ApiController in ASP.NET Core 3.0 returns a validation error:

{"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
 "title":"One or more validation errors occurred.",
 "status":400,
 "traceId":"|9dd96d96-4e64bafba4ba0245.",
 "errors":{"$":["The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1."]}}

when the client post data with content-type : application/json

How do I get the raw json data as a string in my api controller in .NET Core 3.0? Without the client having to update its content type?


Solution

  • Not sure this help but I think they made some change in .net core 3.0 Newtonsoft.JSON package so you can try this

    Install Microsoft.AspNetCore.Mvc.NewtonsoftJson package.

    In your startup.cs add

    services.AddControllers().AddNewtonsoftJson();