Asp.net-Web-Api

ExceptionMessage”:“沒有 MediaTypeFormatter 可用於從“郵遞員”中媒體類型為“multipart/form-data”的內容中讀取“使用者”類型的對象

  • June 19, 2017

我正在使用 Web API 創建登錄服務。當我檢查fiddler它工作正常但當我檢查它postmanchrome顯示錯誤:

{    "Message": "An error has occurred.",
   "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'user' from content with media type 'multipart/form-data'.",
   "ExceptionType": "System.InvalidOperationException",
   "StackTrace": "   
   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n
   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n
   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n
   at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)\r\n
   at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)\r\n
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()\r\n
   at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)" 
}
public class user
{
   //public user(string userId, string password)
   //{
   //    UserId = userId;
   //    Password = password;
   //}

   public string UserId { get; set; }
   public string Password { get; set; }
}

我正在檢查postman此服務是否能夠在移動應用程序中訪問。另外,在mac系統中檢查這個。

點擊標題按鈕,鍵入標題和值

Content-Type: application/json

檢查下面的連結以獲取小提琴和郵遞員都使用相同的內容類型 在 POST 中將 json 發送到 Web API 服務時出錯

我遇到了同樣的問題,但後來我發現我正在使用 ajax POST 呼叫 WebAPI,而不是將其更改為 GET 並且它有效。但錯誤主要是誤導。所以我一直專注於媒體類型。

此資源不支持請求實體的媒體類型“text/plain”。",

$http({             
method: 'POST',

將其更改為

$http({             
method: '**GET**',

引用自:https://stackoverflow.com/questions/24054590