我正在使用Web API创建登录服务。当我检查fiddler
时它工作正常,但当我检查chrome
的postman
时,它显示错误:
{ "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)rn
at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)rn
at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)rn
at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)rn
at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)rn
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()rn
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系统中检查这个
点击Header按钮,键入Header和value
Content-Type: application/json
检查下面的链接,小提琴和邮差都使用相同的内容类型在POST中向web API服务发送json错误
我遇到了同样的问题,但后来我发现我正在调用WebAPI使用ajax POST,我把它改为GET,它工作了。但错误大多是误导。所以我一直专注于媒体类型。
请求实体的媒体类型'text/plain'不支持此资源",
$http({
method: 'POST',
改为
$http({
method: '**GET**',