未設(shè)置[FromForm]致使未能接收到值
環(huán)境:前端是微信小程序,后端是.net6 webApi
前端代碼:
wx.request({
url:?app.globalData.serverHttpUrl?+?"/Comment/",
?method:?"DELETE",
?data:?{CommentId:?e.currentTarget.dataset.commentid,?},
header:?{'content-type':?'application/x-www-form-urlencoded'},
success:?(res)?=>?{}
})
后端代碼:
[HttpDelete]
public async Task<JsonResult> DeleteAsync([FromForm] int CommentId)
?{
? ? if (await _DataCommentRep.DelOneByIdAsync(CommentId) < 1){
? ? ? ?return new JsonResult(new VMResult<string>() { ResultCode = -1, ResultMsg = "S:刪除失敗", ResultEntity = null });?}
? ? ??return new JsonResult(new VMResult<string>() { ResultCode = 0, ResultMsg = "S:刪除成功", ResultEntity = null });
??}
注:如后端不使用 “[FromForm]” 則?CommentId 值始終為 0 即取不到值。
此外,需注意后端為 DELETE或GET時(shí),前端如不使用URL傳值的方式,應(yīng)設(shè)'content-type':?'application/x-www-form-urlencoded'