C# webapi中的POST请求#
我是ASP.net的新手。 我对POST请求有问题。也许我的代码错了,我找不到在哪里 当我尝试post请求时,Postman会向我发送错误消息: “ExceptionType”:“System.Net.Http.UnsupportedMediaTypeException”,“StackTrace”:“a System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent内容,类型类型,IEnumerableC# webapi中的POST请求#,c#,asp.net,post,asp.net-web-api2,C#,Asp.net,Post,Asp.net Web Api2,我是ASP.net的新手。 我对POST请求有问题。也许我的代码错了,我找不到在哪里 当我尝试post请求时,Postman会向我发送错误消息: “ExceptionType”:“System.Net.Http.UnsupportedMediaTypeException”,“StackTrace”:“a System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent内容,类型类型,IEnumerable1格式化程序,IFormat
1格式化程序,IFormatterLogger格式化程序记录器,CancellationToken CancellationToken)\r\náSystem.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable
1格式化程序,IFormatterLogger格式化程序记录器,CancellationToken CancellationToken)”)
这是我的密码:
// POST: api/Boooks
[Route("~/api/PostCreate")]
public IHttpActionResult Post([FromBody] Books books)
{
if (!ModelState.IsValid)
return BadRequest("Invalid data.");
如果您将其保留为默认值,那么您可能会发现您需要在post man中设置应用程序内容类型作为标题信息的一部分 默认情况下,我认为asp.NETCore希望内容类型为application/json。您的post man调用很可能以text/html的形式发送
查看发送的邮件头,进行更改并查看结果。在Postman中检查您的请求邮件头
您不必更改c代码中的任何内容,它默认接受JSON。您是否在postman中添加了“内容类型”:“应用程序/JSON”标题?这是否回答了您的问题?