.net core WebApi将内容类型指定为application/json
我试图返回从第三方API检索到的JSON对象.net core WebApi将内容类型指定为application/json,.net-core,asp.net-core-webapi,.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
[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”
我已经尝试添加
[Produces("application/json")]
但这导致了我的响应内容的双重序列化。FTR我最终对return语句进行了反序列化和重新序列化。虽然不漂亮,但很管用
var tcgResponse = client.Execute(request);
var r = JsonConvert.DeserializeObject<dynamic>(tcgResponse.Content);
return r;
var tcgressponse=client.Execute(请求);
var r=JsonConvert.DeserializeObject(tcgressponse.Content);
返回r;
为什么不返回json(tcgressponse.Content)?另外,您可以反序列化内容并返回实体(方法的返回类型将是您的实体类型,而不是IActionResult)。我的想法是,我需要通过Web API返回JSON对象,因此反序列化似乎是多余的。我可能错了。对于返回类型,它取决于API
,请与我们共享有关tcgResponse.Content
的屏幕截图,尝试使用邮递员请求/catalog/categories
,并与我们共享响应。是您的图像,请向第三方api
或分类控制器
请求,您可以查看内容。通常,它返回文档
是一个plantext html。尝试替换返回Ok(tcgressponse.Content)代码>使用返回Ok(新的{Id=1,Name=“Test”})代码>,您会得到预期的json吗?