简体   繁体   English

ASP.NET Core 中使用 JSON 的 AngularJS API 请求的方法签名

[英]Method signature in ASP.NET Core for AngularJS API request with JSON

I have the following API request:我有以下 API 请求:

var answeredQuestions = {};
$scope.GetNextQuestion = function (question, value) {

    answeredQuestions[question] = value;
    var data = {
    };
    data["currentStep"] = $scope.currentStep;
    data["answeredQuestions"] = answeredQuestions;

    $http.post("/api/WizardAPI/GetNextQuestion", JSON.stringify(data))
        .then(function (data) {
            $scope.currentStep = data.data;
        }, OnError);
};

This is the API controller:这是 API 控制器:

[HttpPost]
[Route("GetNextQuestion")]
public IActionResult GetNextQuestion([FromBody]string currentStep)
{
    return Ok("test");
}

However, "currentstep" is always empty.但是,“currentstep”始终为空。 If I change the data type to object I get something, so the API call is working.如果我将数据类型更改为对象,我会得到一些东西,所以 API 调用正在工作。 I tried mapping it to a custom class, but then the API call fails.我尝试将其映射到自定义类,但 API 调用失败。

Here's the class:这是课程:

public class data
{
    public string CurrentStep { get; set; }
    public string[,] AnsweredQuestions { get; set; }
}

What is the best way to handle this JSON?处理这个 JSON 的最佳方法是什么?

Request payload请求有效载荷

{"currentStep":"ERP","answeredQuestions":{"ERP":1}}

As discussed based on your request payload, the AnsweredQuestions type should be: Dictionary<string, dynamic> .正如根据您的请求负载所讨论的那样, AnsweredQuestions类型应为: Dictionary<string, dynamic>

And it is suggested to name the class as PascalCase as C# naming convention.并且建议将类命名为 PascalCase 作为 C# 命名约定。

public class Data
{
    public string CurrentStep { get; set; }
    public Dictionary<string, dynamic> AnsweredQuestions { get; set; }
}

Change your GetNextQuestion API method as:将您的GetNextQuestion API 方法更改为:

[HttpPost]
[Route("GetNextQuestion")]
public IActionResult GetNextQuestion([FromBody] Data data)
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每个请求的ASP.NET Core API JSON serializersettings - ASP.NET Core API JSON serializersettings per request asp.net core api PUT 请求从不命中方法 - asp.net core api PUT request never hits method asp.net core 3.1 上的 Json 请求 - Json request on asp.net core 3.1 ASP.NET Core ASW S3 - 我们计算的请求签名与您提供的签名不匹配 - ASP.NET Core ASW S3 - The request signature we calculated does not match the signature you provided 在ASP.Net Core 2 Web API发生异常时捕获JSON请求 - Capture JSON request when exception on ASP.Net Core 2 Web API 如何使用 asp.net core 在 JSON web api 请求中捕获包含的属性名称? - How can I capture included property names in a JSON web api request with asp.net core? "ASP.NET Core 3.1:Web Api:相同的发布方法:多种类型的 Json 对象" - ASP.NET Core 3.1: Web Api: Same Post Method: Multiple types of Json Objects Exception when serializing polymorphic c# class with method hiding to json, ASP.NET core API - Exception when serializing polymorphic c# class with method hiding to json, ASP.NET core API 如何在 post 方法中反序列化 asp.net 核心 api 中的 json 数组? - how to deserialize json array in asp.net core api in post method? ASP.Net Core 3.1 WEB API - Controller 方法不返回正确的 Z0ECD11C1D7A287401F814A8 - ASP.Net Core 3.1 WEB API - Controller method doesn't return correct JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM