不确定我为什么遇到这个问题。我以前做过,但看过以前的项目却没有任何帮助。我确信我错过了配置或其他什么。
我在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对象。
我确信我在这里忽略了一些东西但却无法弄清楚它是什么。谁能看到我错过的东西?
谢谢, ģ
更新:添加了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
答案 0 :(得分:0)
我只是将您的代码粘贴到新项目中,但它失败了。
"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;
}