问题描述
使用.NET Framework 4.7.2时,.NET API中的Web API POST和PUT存在问题
我有一个已经运行了几年的Web API,最近我们分支了代码以发布新版本。所有的GET仍然可以正常工作,但是PUT和POST现在在Postman和Swagger中变得异常糟糕。
我很机智,我不知道为什么GET可以正常工作,但是PUT和POST告诉我请求包含实体实体,但是没有内容类型标头(415不支持的媒体类型)...但是他们在那里! (请参阅下面的原始请求)
我尝试了标题中Content-Type,Accept等的每种组合。
public class TestOfPutModel
{
public string strValue1 { get; set; }
public string strValue2 { get; set; }
public bool blnValue3 { get; set; }
}
[Route("v1.0/TestPutOfModel/{id}")]
[System.Web.Http.HttpPut]
public HttpResponseMessage TestOfPut(Guid id,[FromBody] TestOfPutModel putMdl)
{
List<TestOfPutModel> returnValue = new List<TestOfPutModel>();
TestOfPutModel pm1;
if (putMdl == null)
{
pm1 = new TestOfPutModel
{
strValue1 = id.ToString(),strValue2 = $"passed in model- str1: null str2: null bln3: null",blnValue3 = true
};
}
else
{
pm1 = new TestOfPutModel
{
strValue1 = id.ToString(),strValue2 = $"passed in model- str1: {putMdl.strValue1} str2: {putMdl.strValue2} bln3: {putMdl.blnValue3}",blnValue3 = true
};
}
TestOfPutModel pm2 = new TestOfPutModel
{
strValue1 = id.ToString(),strValue2 = "just some text value",blnValue3 = false
};
returnValue.Add(pm1);
returnValue.Add(pm2);
return Request.CreateResponse(HttpStatusCode.OK,returnValue);
}
这是fiddler从我的邮递员电话发出的原始请求:
PUT http://localhost:55459/v1.0/TestPutOfModel/1cdc015c-1d76-46f1-a6e0-c8788830f980 HTTP/1.1
Content-Type: application/json; charset=utf-8
Authorization: Basic XXXXXXXXX
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Cache-Control: no-cache
Host: localhost:55459
Accept-Encoding: gzip,deflate,br
Connection: keep-alive
Content-Length: 85
Cookie: ASP.NET_SessionId=2jewzo0zmsgedisv0bdbpt5b
{
"strValue1": "string1111","strValue2": "string2222","blnValue3": true
}
我用招摇和邮递员收到此错误消息:
{
"Message": "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.","ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'TestOfPutModel' from content with media type 'application/octet-stream'.","ExceptionType": "System.Net.Http.UnsupportedMediaTypeException","StackTrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content,Type type,IEnumerable`1 formatters,IFormatterLogger formatterLogger,CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request,CancellationToken cancellationToken)"
}
如果在邮递员中,我将请求正文更改为“ none”,“ form-data”或“ x-www-form-urlencoded”,则该调用有效,但按预期,该模型为空。
{
"strValue1": "1cdc015c-1d76-46f1-a6e0-c8788830f980","strValue2": "passed in model- str1: null str2: null bln3: null","blnValue3": true
},{
"strValue1": "1cdc015c-1d76-46f1-a6e0-c8788830f980","strValue2": "just some text value","blnValue3": false
}
但是,使用'form-data'或'x-www-form-urlencoded'并放入键/值对将导致相同的414 No Content-Type Header错误。
PUT http://localhost:55459/v1.0/TestPutOfModel/1cdc015c-1d76-46f1-a6e0-c8788830f980 HTTP/1.1
Content-Type: application/json; charset=utf-8
Authorization: Basic XXXXXXXX
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Cache-Control: no-cache
Host: localhost:55459
Accept-Encoding: gzip,br
Connection: keep-alive
Content-Length: 42
Cookie: ASP.NET_SessionId=2jewzo0zmsgedisv0bdbpt5b
strValue1=ttt&strValue2=uuu&blnValue3=true
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)