本文介绍了升级到RC MVC4:没有MediaTypeFormatter可从介质类型'不确定'的内容读类型的对象“TestRequestModel”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用MVC4β和我目前正在升级到最近发布的RC版。

I've been using the MVC4 beta and am currently working to upgrade to the recently released RC version.

看样子,模型结合复合请求类型发生了变化,但我无法弄清楚如何/我在做什么错了。

It appears that model-binding complex request types has changed, but I can't figure out how / what I'm doing wrong.

举例来说,假设我有以下API控制器:

For example, say I have the following API controller:

public class HomeApiController : ApiController
{
    public TestModel Get()
    {
        return new TestModel
        {
            Id = int.MaxValue,
            Description = "TestDescription",
            Time = DateTime.Now
        };
    }
}

这产生预期的结果是:

<TestModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/xxxx">
    <Description>TestDescription</Description>
    <Id>2147483647</Id>
    <Time>2012-06-07T10:30:01.459147-04:00</Time>
</TestModel>

现在说我只是改变了签名,以在请求类型,就像这样:

Now say I just change the signature, taking in a request type, like this:

public TestModel Get(TestRequestModel request)
{
    ...

public class TestRequestModel
{
    public int? SomeParameter { get; set; }
}

我现在得到以下错误:

I now get the following error:

<Exception xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/System.Web.Http.Dispatcher">
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <Message>
        No MediaTypeFormatter is available to read an object of type 'TestRequestModel' from content with media type ''undefined''.
    </Message>
    <StackTrace>
    at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger) at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger) at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken) at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)
    </StackTrace>
</Exception>

我已经看了其中,此异常抛出的来源$ C ​​$ c中的 HttpContentExtensions ,但它看起来像它会检查内容头(这是我应该有),如果它没有,它试图让从 MediaTypeFormatter 收集它对于特定类型的格式化程序(它不能),然后抛出

I've looked at the source code of where this exception is thrown in the HttpContentExtensions, but it looks like it checks for content headers (which I should have), and if it doesn't have that it tries to get a formatter from the MediaTypeFormatter collection it has for the specific type (which it can't) and then throws.

任何人都经历过这样?一些全球注册我的思念?

推荐答案

我看你原来的问题得到回答,但回答另外一个,模型绑定已在RC有所改变。

I see your original question was answered, but to answer the other one, Model binding has changed somewhat in the RC.

http://weblogs.thinktecture.com/cweyer/2012/06/aspnet-web-api-changes-from-beta-to-rc.html

该链接有关于它的一些细节。但是总结一下,似乎会影响你的变化,模型绑定无论从身体,或请求的URI拉它的价值。这是previous版本也是如此,但与候选发布版,MVC4将,默认情况下,放眼机构复杂类型和值类型的URI。

This link has some details about it. But to sum up the change that appears to be affecting you, Model binding pulls its values from either the body, or the uri of the request. This is true for previous releases as well, but with the release candidate, MVC4 will, by default, look to the body for complex types, and the uri for value types.

所以,如果你提交一个机构,包含SomeParameter键,你的要求,你应该看到它进行绑定。或者你可以用URL如果更改声明绑定:

So, if you submit a body with your request containing the "SomeParameter" key, you should see it bind. Or you could bind with the url if you change the declaration to:

 public TestModel Get(int? someParameter)
 {

 }

值得庆幸的是,球队预见到这种潜在的问题,并给我们留下,我们可以用它来重写此行为的属性。

Thankfully, the team foresaw the potential problems with this and left us with attributes we could use to override this behavior.

 public TestModel Get([FromUri]TestRequestModel request)
 {

 }

这里的关键是 [FromUri] 告诉模型绑定中的URI值看。还有 [FromBody] 如果你想要把一个值类型的请求的主体。

The key here is the [FromUri] which tells the model binder to look in the uri for the values. There is also [FromBody] if you want to put a value type in the body of a request.

这篇关于升级到RC MVC4:没有MediaTypeFormatter可从介质类型'不确定'的内容读类型的对象“TestRequestModel”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:33