【发布时间】:2016-01-09 16:39:47
【问题描述】:
我正在尝试在 asp.net web api 上使用 ajax 调用,我想在其中为请求模型属性名称起别名。
但是,当我将值发布回服务器并作为请求接收时,它无法按预期工作。我打算实现的目标如下:
当我将数据 valueA 传递回 web api 时,它将映射到 MemberName。不知道我哪里做错了。
这是我所期望的。
$.ajax({
url: '..',
dataType: 'json',
type: 'POST',
data: { 'valueA': 'ABC' },
success: {}
})
public class MemberProfile {
[JSONProperty('valueA')]
public string MemberID { get; set; }
}
[HttpPost]
public HttpResponseMessage GetMemberProfile(MemberProfile request)
{
}
【问题讨论】:
-
当您点击该操作时,
request是否为空? -
@Glitch100 是的,当点击那个动作时请求 null
-
为了测试,如果您删除
[JSONProperty]并尝试发布MemberId,它会被映射吗? -
@Glitch100 是的,它映射了
-
我很遗憾以前遇到过这个问题,但我能够使用我提到的东西来解决它。尝试更新 newtonsoft?另外你使用的是什么版本的 MVC/WebAPI?
标签: ajax asp.net-mvc asp.net-web-api asp.net-web-api2