我一直在使用MVC4β和我目前正在升級到最近發佈的RC媒體類型」內容閱讀類型的對象「TestRequestModel」版。升級到RC MVC4:沒有MediaTypeFormatter可從與「未定義「」
看起來模型綁定的複雜請求類型已經改變,但我無法弄清楚我在做什麼/我做錯了什麼。
例如,假設我有以下API控制器:
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>
現在說我只是改變了簽名,以在請求的類型,比如此:
public TestModel Get(TestRequestModel request)
{
...
public class TestRequestModel
{
public int? SomeParameter { get; set; }
}
我現在得到以下錯誤:
<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>
我已經看了其中該異常在HttpContentExtensions
拋出的源代碼,但它看起來像它會檢查內容頭(我應該有),如果它沒有,它試圖從MediaTypeFormatter
集合中獲得一個格式化程序,它具有特定的類型(不能),然後拋出。
任何人都經歷過這樣?我錯過了一些全球註冊?
在提琴手中的HTTP請求是什麼?具體是什麼'Content-Type'頭值? – Aliostad
我可以手動傳遞'application/json'的'Content-Type',有趣的是,它讓我過去了那個錯誤(我只是在accept頭中傳遞了application/json)。但現在複雜的類型是空的,這似乎有一些共同的基本問題。 –
那你在接受什麼呢? 'Accept:application/json'不起作用?嘗試在參數上使用'[FromBody]'。 – Aliostad