我有以下 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);
};
這是 API 控制器:
[HttpPost]
[Route("GetNextQuestion")]
public IActionResult GetNextQuestion([FromBody]string currentStep)
{
return Ok("test");
}
但是,“currentstep”始終為空。如果我將資料型別更改為物件,我會得到一些東西,所以 API 呼叫正在作業。我嘗試將其映射到自定義類,但 API 呼叫失敗。
這是課程:
public class data
{
public string CurrentStep { get; set; }
public string[,] AnsweredQuestions { get; set; }
}
處理這個 JSON 的最佳方法是什么?
請求有效載荷
{"currentStep":"ERP","answeredQuestions":{"ERP":1}}
uj5u.com熱心網友回復:
正如根據您的請求有效負載所討論的那樣,AnsweredQuestions型別應該是:Dictionary<string, dynamic>.
并且建議將類命名為 PascalCase 作為 C# 命名約定。
public class Data
{
public string CurrentStep { get; set; }
public Dictionary<string, dynamic> AnsweredQuestions { get; set; }
}
GetNextQuestion將您的API 方法更改為:
[HttpPost]
[Route("GetNextQuestion")]
public IActionResult GetNextQuestion([FromBody] Data data)
{
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/485184.html
標籤:C# asp.net 核心 asp.net-core-webapi
上一篇:為什么沒有有條件地將Swagger服務添加到DI容器中(僅在開發中)?
下一篇:出現Azure錯誤:IDW10203:“范圍”或“scp”宣告不包含范圍“access_as_machine”或未找到
