简体   繁体   English

没有MediaTypeFormatter可用于读取“ InventoryItem”类型的对象

[英]No MediaTypeFormatter is available to read an object of type 'InventoryItem'

AJAX Call AJAX电话

$.ajax({
    url: '/api/Inventory',
    cache: false,
    type: 'POST',
    data: json,
    contentType: 'application/json, charset=utf-8',
    statusCode: {
        201: function (data) {
            console.log(data);
            viewModel.items.push(data);
        }
    }
});

Sent Data ( json ) / Request Payload 发送数据( json )/请求有效负载

{"Id":0,"Upc":"3456789012","Quantity":"200","Category":"Vodka","TransactionType":"Audit","MetaData":"ABSOLUT 750ml"} 

Response Error 响应错误

No MediaTypeFormatter is available to read an object of type 'InventoryItem' from content with media type ''undefined''." 没有MediaTypeFormatter可用于从媒体类型为“未定义”的内容中读取类型为“ InventoryItem”的对象。”

Routed POST method 路由POST方法

public HttpResponseMessage PostItem(InventoryItem item)

All properties in the JSON string are present in the InventoryItem model. JSON字符串中的所有属性都存在于InventoryItem模型中。

A similar question regarding complex types suggested upgrading from Beta to RC to fix a model binding change, which I have done. 关于复杂类型的类似问题建议从Beta升级到RC以修复模型绑定更改,我已经完成了。

If the question isn't obvious, how do I rectify this error? 如果问题不明显,如何纠正此错误? If I add the the [FromUri] attribute to the Routed POST method, then the AJAX call is routed properly, but with an empty InventoryItem . 如果将[FromUri]属性添加到R​​outed POST方法中,则AJAX调用将正确路由,但InventoryItem为空。 If you need any other information, please let me know. 如果您需要其他任何信息,请告诉我。

contentType: 'application/json, charset=utf-8',

should be: 应该:

contentType: 'application/json; charset=utf-8',

Notice the usage of ; 注意的用法; instead of , which is the correct separator between the content type and the charset. 而不是,这是内容类型和字符集之间的正确分隔符。 Also if you follow standard RESTful conventions your controller action should be called Post and not PostItem as you have shown: 同样,如果遵循标准的RESTful约定,则控制器动作应称为Post而不是PostItem如所示:

public HttpResponseMessage Post(InventoryItem item)
{
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有MediaTypeFormatter可用于从媒体类型为“文本/纯文本”的内容中读取“产品”类型的对象 - No MediaTypeFormatter is available to read an object of type 'Product' from content with media type 'text/plain' 没有MediaTypeFormatter可用于从媒体类型为“ multipart / form-data”的内容中读取“ HttpRequestMessage”类型的对象 - No MediaTypeFormatter is available to read an object of type 'HttpRequestMessage' from content with media type 'multipart/form-data' 没有 MediaTypeFormatter 可用于从媒体类型为“text/plain”的内容中读取“String”类型的对象 - No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'text/plain' 没有MediaTypeFormatter可用于从媒体类型为“ application / octet-stream”的内容中读取“ StreamContent”类型的对象 - No MediaTypeFormatter is available to read an object of type 'StreamContent' from content with media type 'application/octet-stream' 没有MediaTypeFormatter可用于在asp.net web api中读取“Advertisement”类型的对象 - No MediaTypeFormatter is available to read an object of type 'Advertisement' in asp.net web api 具有简单表单主体字符串参数抛出的HttpPost没有MediaTypeFormatter可用于读取Exception类型的对象 - HttpPost with simple form-body string parameter throwing No MediaTypeFormatter is available to read an object of type Exception ASP.NET Web API - 没有“MediaTypeFormatter”可用于读取“Int32”类型的对象 - ASP.NET Web API - No 'MediaTypeFormatter' is available to read an object of type 'Int32' 没有 MediaTypeFormatter 可用于从 asp.net mvc webapi 中媒体类型为“text/html”的内容中读取“IEnumerable`1”类型的对象 - No MediaTypeFormatter is available to read an object of type 'IEnumerable`1' from content with media type 'text/html in asp.net mvc webapi 没有MediaTypeFormatter可用'text / html' - No MediaTypeFormatter is available 'text/html' Web令牌认证-没有可用的MediaTypeFormatter - Web Token Authenication - No MediaTypeFormatter available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM