简体   繁体   English

API 在 ASP.NET 核心中返回不受支持的媒体类型

[英]API returning unsupported media type in ASP.NET Core

I have the following API when I do a request to it, it returns unsupported media type I have no idea what is causing this problem.当我向它发出请求时,我有以下 API,它返回不受支持的媒体类型我不知道是什么导致了这个问题。

[HttpPost("select/manager/{projectId}")]
public async Task<ActionResult<string>> SetProjectManager([FromBody] string name, string projectId)
{
    var (isValid, username, password) = spManager.GetCredentials(HttpContext.Request.Headers["x-token"]);

    if (!isValid || string.IsNullOrEmpty(name))
    {
        return BadRequest("Bad Key!");
    }

    var result = await spManager.SetProjectManager(username, password, name, projectId);

    if (result.Success == true)
    {
        return Ok(new SubmissionResponse { Success = true, ErrorCode = null });
    }
    else
    {
        return NotFound(new SubmissionResponse { Success = false, ErrorCode = "Not Found!" });
    }
}

I am sending a token in the header but it's not shown in the screenshot:我在 header 中发送一个令牌,但屏幕截图中没有显示:

在此处输入图像描述

replace "cristof" with {"name": "cristof"} and change the body type to Json.用 {"name": "cristof"} 替换 "cristof" 并将主体类型更改为 Json。

when I do a request to it, it returns unsupported media type当我向它发出请求时,它返回不受支持的媒体类型

The HTTP 415 Unsupported Media Type error indicates that the server refuses to accept the request because the payload format is in an unsupported format.HTTP 415 Unsupported Media Type错误表示服务器拒绝接受请求,因为负载格式是不受支持的格式。 And please note that it doesn't provide a built-in input formatter for plain text in ASP.NET Core Web APIs.请注意,它不为 ASP.NET 核心 Web API 中的纯文本提供内置输入格式化程序。

The framework provides built-in input and output formatters for JSON and XML.该框架为 JSON 和 XML 提供内置输入和 output 格式化程序。 To make the request with your data work well, as @Nkosi suggested, you can try to change the Content-Type to application/json .正如@Nkosi 建议的那样,为了使您的数据请求正常工作,您可以尝试将Content-Type更改为application/json

在此处输入图像描述

Besides, if you really want your APIs work well with plain text input, you can try to implement a custom plain text input formatter.此外,如果您真的希望您的 API 能够很好地处理纯文本输入,您可以尝试实现自定义纯文本输入格式化程序。

https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/custom-formatters?view=aspnetcore-5.0 https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/custom-formatters?view=aspnetcore-5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不支持的媒体类型 ASP.NET Core Web API - Unsupported media type ASP.NET Core Web API ASP.NET 核心 Web API 不支持的媒体类型 - ASP.NET Core Web API unsupported Media type 415 不支持的媒体类型 asp.net 核心 - 415 Unsupported Media Type asp.net core ASP.NET 核心 WebAPI:不支持的媒体类型 - HTTP 415 - ASP.NET Core WebAPI: Unsupported Media Type - HTTP 415 ASP.Net Core Web API:GET 请求不支持的媒体类型 - ASP.Net Core Web API: Unsupported Media Type on GET Request Web API ASP.NET 核心发布请求 415 不支持的媒体类型 - Web API ASP.NET Core Post Request 415 Unsupported Media Type ASP.NET Core 中内容类型“application/csp-report”的“415 Unsupported Media Type” - "415 Unsupported Media Type" for Content-Type "application/csp-report" in ASP.NET Core ASP.NET 核心表单 POST 结果为 HTTP 415 Unsupported Media Type 响应 - ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response .NET Core API 中的 Twilio StatusCallBack &amp; Gather POST 方法返回 HTTP 415 - 不支持的媒体类型 - Twilio StatusCallBack & Gather POST method in .NET Core API returning HTTP 415 - Unsupported Media Type 远程服务器返回错误:(415)在asp.net中使用API​​的不支持的媒体类型 - The remote server returned an error: (415) Unsupported Media Type using API in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM