Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# webapi中的POST请求#_C#_Asp.net_Post_Asp.net Web Api2 - Fatal编程技术网

C# webapi中的POST请求#

C# 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

我是ASP.net的新手。 我对POST请求有问题。也许我的代码错了,我找不到在哪里

当我尝试post请求时,Postman会向我发送错误消息:

“ExceptionType”:“System.Net.Http.UnsupportedMediaTypeException”,“StackTrace”:“a System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent内容,类型类型,IEnumerable
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”标题?这是否回答了您的问题?