栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

MVC Web API,错误:无法绑定多个参数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MVC Web API,错误:无法绑定多个参数

参考:ASP.NET Web API中的参数绑定-使用[FromBody]

最多允许一个参数从消息正文中读取。 因此,这将不起作用

// Caution: Will not work!  public HttpResponseMessage Post([FromBody] int id, [FromBody] string

name) { … }

此规则的原因是请求正文可能存储在只能读取一次的非缓冲流中。

重点矿

话虽如此。您需要创建一个模型来存储预期的聚合数据。

public class AuthModel {    public string userName { get; set; }    public string password { get; set; }}

然后更新动作以期望模型在体内

[HttpPost]public IHttpActionResult GenerateToken([FromBody] AuthModel model) {    string userName = model.userName;    string password = model.password;    //...}

确保正确发送有效载荷

var model = { userName: "userName", password: "password" };$.ajax({    cache: false,    url: 'http://localhost:14980/api/token/GenerateToken',    type: 'POST',    contentType: "application/json; charset=utf-8",    data: JSON.stringify(model),    success: function (response) {    },    error: function (jqXhr, textStatus, errorThrown) {        console.log(jqXhr.responseText);        alert(textStatus + ": " + errorThrown + ": " + jqXhr.responseText + "  " + jqXhr.status);    },    complete: function (jqXhr) {    },})


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386119.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号