Devin Andres Salemi
Devin Andres Salemi

Reputation: 2206

webapi: Error CS0029

Error CS0029 Cannot implicitly convert type 'Microsoft.Net.Http.Headers.MediaTypeHeaderValue' to 'System.Net.Http.Headers.MediaTypeHeaderValue'

I am in .net Core,

the code:

[HttpGet]
public HttpResponseMessage GetCompanies()
{

     var resp = new HttpResponseMessage { Content = new 
    StringContent("[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},
    {\"C\":\"3\"}]]", System.Text.Encoding.UTF8, "application/json") };


    resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    return resp;
}

Upvotes: 2

Views: 1370

Answers (1)

Nkosi
Nkosi

Reputation: 247471

Core does not use HttpResponseMessage on the server side. You need to update your syntax.

[HttpGet]
public IActionResult GetCompanies() {
    var json = "[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]";
    return Content(json, new MediaTypeHeaderValue("application/json"));
}

Upvotes: 1

Related Questions