I have an application that was written in aspnet core 2.1 and I did an upgrade to core 3.1 the web API failed. Why is JToken now not valid parameter?
// This works on core 2.1 but failed on 3.1
[HttpPost] public async Task<IActionResult> PostAsync([FromBody] JToken value) {
// code here
return Ok();
}
// This works with Core 3.1
[HttpPost] public async Task<IActionResult> PostAsync([FromBody] JObject jvalue) {
// code here
return Ok();
}
//error return
{"errors":{"":["Unexpected character encountered while parsing value: {. Path '', line 1, position 1."]},"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|8ab7b30f-40b939694d56c1ec."}
As part of the work to improve the ASP.NET Core shared framework, Newtonsoft.Json has been removed from the ASP.NET Core shared framework for asp.net core 3.x.
To reuse JToken,follow the steps below:
Install theMicrosoft.AspNetCore.Mvc.NewtonsoftJsonpackage on nuget.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Yes, that is what I did. The JObject and JToken belongs to Newtonsoft library. What I am confused is that in Core 3.1 JToken cannot be used as the parameter of the "Post", it just errors out during runtime.
May I know if there is an equivalent for the the two classes in System.Text.Json ?
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
6 Points
48 Posts
Jtoken as Post Parameter in aspnet Core 3.1
Jun 25, 2020 04:08 AM|Albertk89|LINK
I have an application that was written in aspnet core 2.1 and I did an upgrade to core 3.1 the web API failed. Why is JToken now not valid parameter?
// This works on core 2.1 but failed on 3.1
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] JToken value)
{
// code here
return Ok();
}
// This works with Core 3.1
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] JObject jvalue)
{
// code here
return Ok();
}
//error return
{"errors":{"":["Unexpected character encountered while parsing value: {. Path '', line 1, position 1."]},"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|8ab7b30f-40b939694d56c1ec."}
Contributor
2690 Points
874 Posts
Re: Jtoken as Post Parameter in aspnet Core 3.1
Jun 25, 2020 06:38 AM|Rena Ni|LINK
Hi Albertk89,
As part of the work to improve the ASP.NET Core shared framework,
Newtonsoft.Json
has been removed from the ASP.NET Core shared framework for asp.net core 3.x.To reuse JToken,follow the steps below:
Install the
Microsoft.AspNetCore.Mvc.NewtonsoftJson
package on nuget.Update
Startup.ConfigureServices
to callAddNewtonsoftJson
.Reference:
https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#newtonsoftjson-jsonnet-support
Best Regards,
Rena
Member
6 Points
48 Posts
Re: Jtoken as Post Parameter in aspnet Core 3.1
Jun 25, 2020 06:43 AM|Albertk89|LINK
Yes, that is what I did. The JObject and JToken belongs to Newtonsoft library. What I am confused is that in Core 3.1 JToken cannot be used as the parameter of the "Post", it just errors out during runtime.
May I know if there is an equivalent for the the two classes in System.Text.Json ?
Contributor
2690 Points
874 Posts
Re: Jtoken as Post Parameter in aspnet Core 3.1
Jun 25, 2020 09:25 AM|Rena Ni|LINK
Hi Albertk89,
I think you could try JsonDocument,refer to:
https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#jsondocument-and-jsonelement-compared-to-jtoken-like-jobject-jarray
Best Regards,
Rena