发送 JSON-LD 时不支持的媒体类型格式化程序



我正在使用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());

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

有人知道解决方案吗?

查看文档

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

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

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

相关内容

最新更新