WebApi将Content-Type指定为application / json

时间:2018-09-02 05:50:07

标签: .net-core asp.net-core-webapi

我正在尝试返回从第三方API检索到的JSON对象。

[Route("api/Catalog/Categories")]
public class CategoriesController : Controller
{
    //Get all categories
    [HttpGet]
    public IActionResult Get()
    {

        var client = new RestClient();
        client.BaseUrl = new Uri("http://api.tcgplayer.com");

        var request = new RestRequest(Method.GET);
        request.Resource = "/catalog/categories";
        request.RequestFormat = DataFormat.Json;
        request.AddHeader("Content-Type", "application/json; charset=utf-8");
        request.AddHeader("Authorization", "Bearer redacted");

        var tcgResponse = client.Execute(request);

        return Ok(tcgResponse.Content);
    }
}

内容类型显示为“文档”,这是不希望的。如何将内容类型指定为“ application / json”?

chrome network tab

chrome response headers

我已经尝试添加

[Produces("application/json")]

但这会导致我的回复内容进行两次序列化。

1 个答案:

答案 0 :(得分:0)

FTR我最终对return语句进行了反序列化和重新序列化。不漂亮,但是有效。

 var tcgResponse = client.Execute(request);

 var r = JsonConvert.DeserializeObject<dynamic>(tcgResponse.Content);

 return r;