为什么后续参数不需要 FromBody 修饰符?

问题描述

jQuery 通过 ajax post 调用 mvc 控制器。这有效,但为什么第二个参数也不需要 [FromBody] 修饰符?

其中 itemList 是一个 int 数组,而 billingStatus 是一个字符串。

jQuery

$.ajax({
        url: '@Url.Action("UpdateBillingLines","BillingExceptions")',data: {
            lines: itemList,statusTo: billingStatus
        },type: 'POST',async: false,success: function (data) {
            //test
        },error: function (xhr,txtStatus,err) {
            alert(txtStatus);
        }
 });

控制器

[System.Web.Http.HttpPost]
public ActionResult UpdateBillingLines([FromBody] List<int> lines,string statusTo)
{
    string result = "";
    // logic here
    return Json(result,JsonRequestBehavior.AllowGet);
}

解决方法

并不是不需要(正如你所说)。就是它不能用于后面的参数。 documentation 解释了原因:

最多允许从消息正文中读取一个参数... 此规则的原因是请求正文可能存储在 只能读取一次的非缓冲流