ASPNet WebAPI:POST上的JSON反序列化错误

时间:2012-05-04 01:52:22

标签: jquery asp.net-web-api

更新:此问题可能是由于我对SPA模板和技术缺乏了解。我试图忽略upshot库并在此错误之前对模板进行了大量手动修改。请投票以非建设性的方式结束。

我正在尝试使用真实世界类型场景的ASP.Net MVC4 SPA项目。由于upshot还没有,我在客户端使用WebAPI部分和Knockout以及JQuery Ajax方法。

在我必须使用任意参数发布数据之前,一切正常:

这是我的控制者:

public class ProductLineController : DbDataController<WebSiteContext>
{
    [HttpPost]
    public HttpResponseMessage<Order> AddProductLine(int productId, int orderId)
    {
         [invoke logic to add the product line]
         [return the updated order]
    }
}

调用此控制器的客户端代码位于Knockout视图模型上,如下所示:

this.addProduct = function (product) {
    var prodId = product.ProductId;

    var requestUrl = baseUrl + "productline";
    $.ajax({
        url: requestUrl,
        cache: false,
        type: 'POST',
        data: JSON.stringify({productId: prodId, orderId: orderId}),
        dataType:"json",
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            self.order(data);
        }
    });

非常直截了当。在对类似问题进行一些调查后,我采取了预防措施来对json参数数据进行字符串化。

Fiddler报告的请求标题是:

POST http://localhost:50045/api/orderedproduct HTTP/1.1
Host: localhost:50045
Connection: keep-alive
Content-Length: 27
Origin: http://localhost:50045
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) [snipped]
Content-Type: application/json; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:50045/Order/Create
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8,en-US;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{"productId":1,"orderId":1}

正如您所注意到的,JSON格式正确。但回应是一个杀手500

System.Runtime.Serialization.SerializationException
There was an error deserializing the object of type System.Int32. The value '' cannot be parsed as the type 'Int32'.

诅咒堆栈跟踪比这更长,但这是重要的部分。

你能告诉我这里的错误是什么吗?我迷路了。

0 个答案:

没有答案
相关问题