【问题标题】:Unsupported media type formatter when sending JSON-LD发送 JSON-LD 时不支持的媒体类型格式化程序
【发布时间】:2020-01-25 20:29:45
【问题描述】:

我正在使用 C# 中的 HttpClient 将 JSON LD 数据发送到 webapi 端点。 我收到错误不支持的媒体类型格式化程序。 我发送的数据是 JSONLD 作为字符串:

string data = @"{
                      ""@type"": ""vcx:blah"",
                      ""vcx:key"": ""blah"",
                      ""vcx:value"": ""blah""
                 } ";
httpClient.PostAsync(uri, new StringContent(data));

在 api 方面,是 web api。在 Global.asax.cs 我有以下内容:

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new 
JsonMediaTypeFormatter());

即使我删除了格式化程序的清除,我在调用应用程序上也会遇到同样的错误。

有人知道解决办法吗?

【问题讨论】:

    标签: c# asp.net-web-api json-ld mediatypeformatter


    【解决方案1】:

    docs

    您使用StringContent的第一个构造函数,这意味着StringContent创建的媒体类型默认为text/plain

    如果您需要 jsono,请指定媒体类型:

    httpClient.PostAsync(uri, new StringContent(data, Encoding.UTF8, "application/json"));
    

    【讨论】:

    • 这使我现在可以访问 api 端点,但是字符串参数为空。这是我的端点签名: public HttpResponseMessage PostJSONLD([FromBody] string request)
    • 实际上,如果我在 api 端使用对象而不是字符串作为参数,我会填充它。
    • 如果您有新问题,请点击 按钮提出问题。如果有助于提供上下文,请包含指向此问题的链接
    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多