不知道为什么我有这个问题。我以前做过,但查看以前的项目没有任何帮助。我确定我缺少配置或其他东西。
我在 WebForms 4.5 项目中有一个 ApiController,用于从数据存储区(SQL Server CE 4.0)检索和更新数据。
我已经定义了以下 POST 方法来处理向产品添加属性记录...
[HttpPost]
public void AddPropertyToProduct([FromBody]ProductPropertyViewModel prodProp)
{
Mapper.CreateMap<ProductPropertyViewModel, ProductProperty>();
ProductProperty property = Mapper.Map<ProductProperty>(prodProp);
ProductRepository.Instance.AddProperty(property.ProductId, property);
ProductRepository.Instance.SaveChanges();
}
ProductPropertyViewModel 参数是我使用的 ViewModel 而不是数据模型...
public class ProductPropertyViewModel
{
public Int64 Id { get; set; }
public Int64 ProductId { get; set; }
public String PropertyName { get; set; }
public String PropertyValue { get; set; }
public String Comments { get; set; }
public DateTime DateAdded { get; set; }
public DateTime DateUpdated { get; set; }
}
调用该服务的客户端脚本发送一个 JSON 字符串,该字符串与属性的 ViewModel 属性相匹配...
var addPropertyToProduct = function (propertyViewModel, productId, isAsync, fnSuccess, fnError) {
var methodUrl = "/api/ProductPropertyAPI/AddPropertyToProduct/"
var ret = null;
//
// Make sure that the ProductId is specified as part of the ViewModel
propertyViewModel.ProductId = productId;
//
// make sure that the propertyViewModel is reduced to JSON for being passed to the server.
// Example : {"Id": 0, "ProductId":5,"PropertyName":"Engine Size","PropertyValue":"300cc","Comments":"Some comment","DateAdded":"07/25/2013","DateUpdated":"07/25/2013"}
var jsonData = ko.toJSON(propertyViewModel);
$.ajax({
type: "POST",
data: jsonData,
async: isAsync,
contentType: 'application/json;charset=utf-8',
url: methodUrl,
success: function (data) {
//#region console log
var logMsg = "AJAX.ProductPropertyAPI.AddPropertyToProduct ( "+ JSON.stringify(jsonData) +" ): RESPONSE : " + JSON.stringify(data);
console.log(logMsg);
//#endregion
if (typeof fnSuccess === 'function') {
fnSuccess(data);
}
else {
defaultSuccess(data);
}
},
error: function (data) {
if (typeof fnError === 'function') {
fnError(data);
}
else {
defaultError(data);
}
}
});
}
我遇到的问题是,当请求发布到服务的AddPropertyToProduct方法时,prodProp参数为 NULL 而不是 ViewModel 的实例。我的理解是 WebAPI 知道将 JSON 数据映射到 ProductPropertyViewModel 对象。
我确信我在这里忽略了一些东西,但无法弄清楚它是什么。谁能看到我错过了什么?
谢谢,G
更新:添加了提琴手数据
这是我通过 Fiddler 发送的请求。
POST http://localhost:55556/api/ProductPropertyAPI/AddPropertyToProduct HTTP/1.1
User-Agent: Fiddler
Host: localhost:55556
ContentType: 'application/json;charset=utf-8'
Content-Length: 155
{"Id": 0, "ProductId":5,"PropertyName":"Engine Size","PropertyValue":"300cc","Comments":"Some comment","DateAdded":"07/25/2013","DateUpdated":"07/25/2013"}
更新:从 IE 开发人员工具中添加了 $.Ajax 请求标头
Key Value
Request POST http://localhost:55556/api/ProductPropertyAPI/AddPropertyToProduct/ HTTP/1.1
Accept */*
Content-Type application/json;charset=utf-8
X-Requested-With XMLHttpRequest
Referer http://localhost:55556/Admin/ProductManager.aspx
Accept-Language en-US
Accept-Encoding gzip, deflate
User-Agent Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Host localhost:55556
Content-Length 129
DNT 1
Proxy-Connection Keep-Alive
Pragma no-cache
Cookie __atuvc=38%7C8; __AntiXsrfToken=63a0ede574be4aa9a19e153474320450; ASPSESSIONIDACBTCRRS=FDDPEJIBPDFEODAHOEMBOOFK; ASPSESSIONIDCABTDTRT=AANBPPFCEFOBMAEFMMPMMFLF; ASPSESSIONIDAAARDSRS=LODHOFGCLJILEEEHICHGLNLA; ASPSESSIONIDCCDTCSQT=NHOJPJGCOECNJBIDGGKIALFG; ASP.NET_SessionId=xh4xctv0mvq2zb454hkb1f3z