相关疑难解决方法(0)

升级到MVC4 RC:没有MediaTypeFormatter可用于从媒体类型为"undefined"的内容中读取"TestRequestModel"类型的对象

我一直在使用MVC4测试版,目前正在努力升级到最近发布的RC版本.

似乎模型绑定复杂请求类型已经改变,但我无法弄清楚我是怎么做错的.

例如,假设我有以下API控制器:

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

这产生了预期的结果:

<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>
Run Code Online (Sandbox Code Playgroud)

现在说我只是更改签名,接受请求类型,如下所示:

public TestModel Get(TestRequestModel request)
{
    ...

public class TestRequestModel
{
    public int? SomeParameter { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我现在收到以下错误:

<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''. …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc model-binding asp.net-mvc-4 asp.net-web-api

8
推荐指数
1
解决办法
7159
查看次数