的ASP.NET Web API - 不“MediaTypeFormatter”可阅读类型的对象“的Int32” [英] ASP.NET Web API - No 'MediaTypeFormatter' is available to read an object of type 'Int32'

查看:672
本文介绍了的ASP.NET Web API - 不“MediaTypeFormatter”可阅读类型的对象“的Int32”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能完全肯定什么发生在这里。我可以在什么地方把事情搞乱,但我不知道是什么。

I'm not entirely sure whats happened here. I may have messed things up somewhere, but I don't know what.

我的API控制器方法是这样的:

My API controller method looks like this:

public HttpResponseMessage<string> Put(int id)

我试过一个字符串,以及具有相同的错误。

I've tried a string as well with the same error.

任何想法?

感谢。

编辑:需要明确的是 - 该ID是路由参数。请求的主体是JSON。如果我删除路由参数,该方法的功能是正常的。

To be clear - the id is the route parameter. The body of the request is JSON. If I remove the route parameter,the method functions as normal.

推荐答案

请阅读下面的博客文章:

Please read following blog post:

  • ASP.NET Web API and Simple Value Parameters from POSTed data

这说明用简单的参数最典型的问题。不知道你的要求看起来如何任何更多的细节无法确定你已经打哪一个。

This describes most typical issues with using simple parameters. Without knowing any more details about how your request looks it is not possible to determine which one you have hit.

更新

还有一个已知的bug考虑路由值。在当建于格式化没有一个用于POST / PUT / PATCH请求的情况下,路由值参数没有被绑定。要解决此最好的解决办法是写ActionFilterAttribute描述如下:

There is one more known bug considering route values. In case when not one of built in formatters is used for POST/PUT/PATCH request, the route values parameters are not being bind. To work around this the best solution is to write ActionFilterAttribute as described below:

这篇关于的ASP.NET Web API - 不“MediaTypeFormatter”可阅读类型的对象“的Int32”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆