8

Previously (In .Net Core 2.1) I successfully handled JSON data with below method

[HttpPost]
[Route("sendJsonData")]
public JObject saveTemplate(JObject jsonString)
{

        string templateName = (string)jsonString.SelectToken("templateName");
        string filePathAndName = "D:\\" + "templates\\" + templateName + ".txt";

        using (StreamWriter file = File.CreateText(@filePathAndName))
        {
            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(file, jsonString);
        }

    return jsonString;
}

But when I created the same method with .Net Core 3.1. It is not working showing some error with JObject. I am getting that JSON from below code

onSubmit() {
this.http.post("https://localhost:44350/ReportAPI/sendJsonData", this.surveyForm.value)
            .subscribe(
                data => console.log("success!", data),
                error => console.error("couldn't post because", error)
            );
}

Below is the error (Response from Postman)

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|6e209814-4dd7396cc2dfa182.",
    "errors": {
        "$": [
            "The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[Newtonsoft.Json.Linq.JToken]. Path: $ | LineNumber: 0 | BytePositionInLine: 14."
        ]
    }
}

JSON

{  
   "templateName":"BFS Survey",
   "surveyQuestions":[  
      {  
         "questionTitle":"Name",
         "questionType":"Text",
         "questionGroup":{  

         }
      },
      {  
         "questionTitle":"Age",
         "questionType":"Number",
         "questionGroup":{  

         }
      },
      {  
         "questionTitle":"Gender",
         "questionType":"Single choice",
         "questionGroup":{  
            "options":[  
               {  
                  "optionText":"Male"
               },
               {  
                  "optionText":"Female"
               },
               {  
                  "optionText":"Other"
               }
            ],
            "showRemarksBox":false
         }
      },
      {  
         "questionTitle":"Skills",
         "questionType":"Multi choice",
         "questionGroup":{  
            "options":[  
               {  
                  "optionText":"Java"
               },
               {  
                  "optionText":"Angular"
               },
               {  
                  "optionText":"Python"
               },
               {  
                  "optionText":"R"
               }
            ],
            "showRemarksBox":false
         }
      }
   ]
}

Code perfectly worked using .Net Core 2.1 but not working with 3.1. Please suggest me how resolve this issue.

9
  • 2
    Can you post that error Commented Dec 5, 2019 at 7:23
  • I have so many questions about this implementation... can you show us the error? usually, a dedicated class is used to deserialize the json from the request body, instead of a plain JObject. Also, why are you catching an error just to throw it again? you could get rid of the entire try catch block, and it would act exactly the same Commented Dec 5, 2019 at 7:40
  • Please see updated question for error. @GlennvanAcker Commented Dec 5, 2019 at 8:12
  • Please see updated question for error. @Rajesh Commented Dec 5, 2019 at 8:12
  • 2
    @RahulSharma It worked!!! Thanks a lot. Really appreciate it. :) Commented Dec 5, 2019 at 8:43

1 Answer 1

17

In order to migrate from ASP.NET Core 2.x to 3.0, refer to this link:

  • Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson
  • Add in Startup.cs, in method ConfigureServices: services.AddMvc().AddNewtonsoftJson();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.