所以,以前我的團隊對所有事情都使用 POST 請求。我終于在說服他們按預期使用動詞方面贏得了戰爭。
GET 用于查詢,
POST 用于創建等。
我們有一個非常復雜的物件,我們需要將其作為 GET 發送:
{
"UserId": "1",
"CategoryId": "39",
"StartMillis": 1609459200000,
"EndMillis": 1635724799000,
"GroupByAttributeTypeId": "105",
"SelectedAttributes": [{
"TypeId": "100",
"AttributeIds": [1]
}, {
"TypeId": "105",
"AttributeIds": [6697]
}
]
}
這就像 POST 一樣簡單,但不如 GET。Action我們創建的方法類似于:
[HttpGet]
[Route(AspNet.Mvc.ActionTemplate)]
public IActionResult GetCompletedAudits([FromQuery]CompletedAuditDto payload)
{
return OkResponse(_auditService.GetCompletedAudit(payload));
}
在哪里CompletedAuditDto:
public class CompletedAuditDto
{
public int UserId { get; set; }
public string CategoryId { get; set; }
public long StartMillis { get; set; }
public long EndMillis { get; set; }
public string GroupByAttributeTypeId { get; set; }
public IEnumerable<TypeAttributeDto> SelectedAttributes;
}
并且TypeAttributeDto是:
public class TypeAttributeDto
{
public int TypeId;
public IEnumerable<int> AttrIds;
}
我已經使用qs庫嘗試了許多不同的序列化。但該SelectedAttributes集合總是為空。我將日志記錄降低到debug級別,模型系結器甚至似乎都沒有嘗試反序列化該物件(沒有提到SelectedAttributes集合)
[14:35:44 DBG] Attempting to bind parameter 'payload' of type 'WebApp.Payloads.Request.CompletedAuditDto' ... [14:35:44 DBG] Attempting to bind parameter 'payload' of type 'WebApp.Payloads.Request.CompletedAuditDto' using the name '' in request data ... [14:35:44 DBG] Attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.UserId' of type 'System.Int32' using the name 'UserId' in request data ... [14:35:44 DBG] Done attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.UserId' of type 'System.Int32'. [14:35:44 DBG] Attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.CategoryId' of type 'System.String' using the name 'CategoryId' in request data ... [14:35:44 DBG] Done attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.CategoryId' of type 'System.String'. [14:35:44 DBG] Attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.StartMillis' of type 'System.Int64' using the name 'StartMillis' in request data ... [14:35:44 DBG] Done attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.StartMillis' of type 'System.Int64'. [14:35:44 DBG] Attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.EndMillis' of type 'System.Int64' using the name 'EndMillis' in request data ... [14:35:44 DBG] Done attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.EndMillis' of type 'System.Int64'. [14:35:44 DBG] Attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.GroupByAttributeTypeId' of type 'System.String' using the name 'GroupByAttributeTypeId' in request data ... [14:35:44 DBG] Done attempting to bind property 'WebApp.Payloads.Request.CompletedAuditDto.GroupByAttributeTypeId' of type 'System.String'. [14:35:44 DBG] Done attempting to bind parameter 'payload' of type 'WebApp.Payloads.Request.CompletedAuditDto'. [14:35:44 DBG] Done attempting to bind parameter 'payload' of type 'WebApp.Payloads.Request.CompletedAuditDto'.
任何想法如何將其序列化為模型系結器會滿意的格式。這是我嘗試過的眾多事情之一:
UserId=1&CategoryId=39&StartMillis=1609459200000&EndMillis=1635724799000&GroupByAttributeTypeId=105&SelectedAttributes[].TypeId=100&SelectedAttributes[].Attribute[].AttributeIds[Attribute]Attribute]Attribute]Attribute]Attribute]Attribute%SelectAttribute[[ 6697
uj5u.com熱心網友回復:
首先,您需要在模型中添加 getter setter,如下所示:
public class CompletedAuditDto
{
public int UserId { get; set; }
public string CategoryId { get; set; }
public long StartMillis { get; set; }
public long EndMillis { get; set; }
public string GroupByAttributeTypeId { get; set; }
public IEnumerable<TypeAttributeDto> SelectedAttributes { get; set; }
}
public class TypeAttributeDto
{
public int TypeId { get; set; }
public IEnumerable<int> AttrIds { get; set; }
}
然后發送如下請求:
UserId=1&CategoryId=39&StartMillis=1609459200000&EndMillis=1635724799000&GroupByAttributeTypeId=105&SelectedAttributes[0].TypeId=100&SelectedAttributes[0].AttrIds=1].5TypeIds=1&SelectedTypeIds9AttrIds=1&Selected6TypeIds[7
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331453.html
上一篇:jwt令牌驗證失敗后授權仍在繼續
下一篇:帶&不帶逗號的數字的正則運算式
