[英]Get traceId from ValidationProblem in ASPNet Core 3.1 Api Controller
[英]Problems with FromFormAttribute and deserialization in AspNet Core 3.1 Api
我使用 Newtonsoft 反序列化器並嘗試以這種方法發送數據(使用 FromFormAttribute):
[HttpPut("test")]
public IActionResult Test([FromForm] MyClass Test) {
return Ok();
}
另外,我有 class:
public class MyClass
{
public int Id { get; protected set; }
public string Text { get; protected set; }
public MyClass(int id, string text) {
Id = id;
Text = text;
}
}
當我嘗試在 HttpPut 方法中發送數據時,我得到
System.InvalidOperationException: Could not create an instance of type 'MyClass'.
Model bound complex types must not be abstract or value types and must have a parameterless constructor.
Alternatively, give the 'test' parameter a non-null default value.
我沒有創建無參數構造函數並保護了 setter,因為它在使用 FromBodyAttribute 的其他方法中工作正常。
我究竟做錯了什么?
只需添加一個無參數構造函數
public class MyClass
{
public int Id { get; set; }
public string Text { get; set; }
public MyClass()
{
}
public MyClass(int id, string text)
{
Id = id;
Text = text;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.