不知道我为什么会有这个问题。我以前也这样做过,但是看看以前的项目就没有得到任何帮助。我确信我遗漏了一个配置什么的。
我在一个ApiController 4.5项目中有一个WebForms,用于从数据存储检索和更新数据(SQLServerCE4.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参数是空的,而不是ViewModel的实例。我的理解是,WebAPI将知道如何将JSON数据映射到ProductPropertyViewModel对象。
我相信我在这里忽略了一些东西,但无法弄清楚它是什么。有人能看到我错过了什么吗?
谢谢你,G
更新:添加Fiddler数据
这是我通过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发布于 2013-07-26 00:22:38
我只是把你的代码复制到新的项目中,结果失败了。
"Message":"An error has occurred.",
"ExceptionMessage":"Object reference not set to an instance of an object.",
"ExceptionType":"System.NullReferenceException"我猜你也犯了同样的错误。但在我搬走之后:
contentType: 'application/json;charset=utf-8',啊,真灵。
我的完整ajax请求:
// I used static jsonData because you're getting the correct values in
// your fiddler so this is definitely not causing any issues
var jsonData = { "Id": 0, "ProductId": 5, "PropertyName": "Engine Size", "PropertyValue": "300cc", "Comments": "Some comment", "DateAdded": "07/25/2013", "DateUpdated": "07/25/2013" };
var methodUrl = "/api/test/AddPropertyToProduct/";
$.ajax({
type: "POST",
data: jsonData,
//async: isAsync,
//contentType: 'application/json;charset=utf-8',
url: methodUrl,
success: function (data) {
console.log('Success: ', data);
},
error: function (data) {
console.log('Error: ', data);
}
});我所用的行动方法:
[HttpPost]
public string AddPropertyToProduct(ProductPropertyViewModel prodProp)
{
return prodProp.PropertyName;
}https://stackoverflow.com/questions/17869344
复制相似问题