2012-06-07 29 views
8

我一直在使用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集合中獲得一個格式化程序,它具有特定的類型(不能),然後拋出。

任何人都經歷過這樣?我錯過了一些全球註冊?

+0

在提琴手中的HTTP請求是什麼?具體是什麼'Content-Type'頭值? – Aliostad

+0

我可以手動傳遞'application/json'的'Content-Type',有趣的是,它讓我過去了那個錯誤(我只是在accept頭中傳遞了application/json)。但現在複雜的類型是空的,這似乎有一些共同的基本問題。 –

+0

那你在接受什麼呢? 'Accept:application/json'不起作用?嘗試在參數上使用'[FromBody]'。 – Aliostad

回答

13

我看你原來的問題得到回答,但回答另外一個,模型綁定已在RC有所改變。

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

此鏈接有一個關於它的一些細節。但是爲了總結看起來影響你的變化,模型綁定從請求的正文或URI中提取它的值。這也適用於以前的版本,但對於候選版本,默認情況下,MVC4將查看複雜類型的主體,以及值類型的uri。

所以,如果你提交身體與包含「SomeParameter」鍵,你的要求,你應該看到它綁定。

public TestModel Get(int? someParameter) 
{ 

} 

值得慶幸的是,球隊預見到這種潛在的問題,並給我們留下了我們可以用它來覆蓋此行爲屬性:或者你可以用URL如果更改聲明綁定。

public TestModel Get([FromUri]TestRequestModel request) 
{ 

} 

這裏的關鍵是[FromUri],它告訴模型聯編程序在uri中查找值。如果您想在請求的主體中放置值類型,也有[FromBody]

+0

感謝您提供其他RC更改的鏈接。我一直在尋找像這樣的全面清單。看起來像我有很多屬性來添加:) –

2

我們看到的是同樣的東西。在我們的例子中,問題是傳入get方法的複雜對象。我們需要在該方法的參數中添加一個[FromUri]屬性。

http://forums.asp.net/t/1809925.aspx/1?GET+requests+with+complex+object+as+input+parameter

public class SearchController : ApiController 
{ 
    // added [FromUri] in beta to RC transition otherwise media type formatter error 
    public IQueryable<SearchResultEventModel> Get([FromUri]SearchSpecModel search) 
    { 
     // ... 
    } 
} 
相關問題