gpt4 book ai didi

json - 将 JSON 发布到 Web API 2 : The request entity's media type 'text/plain' is not supported for this resource 时出错

转载 作者:行者123 更新时间:2023-12-04 01:25:42 34 4
gpt4 key购买 nike

我有这门课:

public class MyClass
{
public MyClass() { Secret = "Don't tell me"; }
public string Name { get; set; }
public int Age { get; set; }
public string Description { get; set; }
private string Secret { get; set; }
}

而这个 WEB API 方法:
        // POST api/fixture
public HttpResponseMessage Post(MyClass value)
{
return new HttpResponseMessage(HttpStatusCode.Created);
}

我已将 Web API 设置为返回 JSON 而不是 XML,并且我没有对默认配置进行任何其他更改。我正在尝试使用 Firefox 的 RESTClient 扩展来测试此方法。这是我的要求:
POST localhost:XXXX/api/fixture
Content-Type: application/json
Accept: application/json
Content-Length: 60

{
value : { "Name":"Cosby","Age":"13","Description":"OK" }
}

但是我收到了这个错误:

{"Message":"The request entity's media type 'text/plain' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'MyClass' from content with media type 'text/plain'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}



编辑:

我不明白,因为似乎甚至没有调用该方法。 如果我调试,我看到构造函数被调用,然后没有其他方法被调用。没有引发异常。

我已经在这个问题上待了很长时间了。我发现这个问题通常发生在 Content-Type 设置不正确的情况下,但它似乎不是我的情况,因为请求甚至没有被处理。

任何的想法 ?

谢谢

最佳答案

内部 POSTMAN ,您只需将设置更改为 application/json.引用下图。

application/json

关于json - 将 JSON 发布到 Web API 2 : The request entity's media type 'text/plain' is not supported for this resource 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25935650/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com