c# - 如何在 C# (.NET Core 3.1) 中处理 POST 请求中的 JSON 数据

标签 c# .net json angular

以前(在 .Net Core 2.1 中)我使用以下方法成功处理了 JSON 数据

[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;
}

但是当我使用 .Net Core 3.1 创建相同的方法时。它无法显示 JObject 的一些错误。 我从下面的代码中获取 JSON

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)
            );
}

错误如下(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
         }
      }
   ]
}

代码使用 .Net Core 2.1 完美运行,但不适用于 3.1。请建议我如何解决这个问题。

最佳答案

为了从 ASP.NET Core 2.x 迁移到 3.0,请参阅 this link :

  • 添加对 Microsoft.AspNetCore.Mvc.NewtonsoftJson 的包引用
  • 在Startup.cs中,在方法ConfigureServices中添加:services.AddMvc().AddNewtonsoftJson();

关于c# - 如何在 C# (.NET Core 3.1) 中处理 POST 请求中的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59190056/

相关文章:

python - 根据字段名称和值从 JSON 选择字段

json - 如何搜索从 url 获取并使用 httparty 解析的 json 数组?

c# - 使用 MySql 查询 LOAD DATA IN FILE 的方法永远不会完成

c# - 切换字符串的部分

c# - 在另一个 IDisposable "using"语句中处理 IDisposables

c# - Pinvoke 调用总是返回 -1

c# - 使用 Json.NET lib 通过 json 发送 javascript 函数

java - JSON 对象数组到普通旧 java 对象

c# - 如何使用linq to sql在asp.net中显示过滤后的数据

.net - 我如何管理带有 Simple.Data 的 MongoDb 集合中不断变化的数据结构?