I have a .NET Core 2 app that needs to be able to return generated HTML from a controller. I've been able to get it returning the HTML as plain text, but not to persuade the browser that it's HTML and render that; as soon as an HTML content type is provided, the content type negotiation appears to break it and it just renders a 406 Not Acceptable.
(Simplified) options I've tried -
[HttpGet]
[Produces("text/html")]
public string Display()
{
return "<html><head><title>Testing</title><head><body>Hello, world!</body></html>";
}
[HttpGet]
[Produces("text/html")]
public HttpResponseMessage Display()
{
try
{
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("<html><head><title>Testing</title><head><body>Hello, world!</body></html>")
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
[HttpGet]
[Produces("text/html")]
public IActionResult Display()
{
var pageHtml = "<html><head><title>Testing</title><head><body>Hello, world!</body></html>";
var result = StatusCode(200, pageHtml);
result.ContentTypes.Add(new MediaTypeHeaderValue("text/html"));
return StatusCode(200, pageHtml);
}
The Startup.ConfigureServices method has been tried with all combinations I can think of of the RespectBrowserAcceptHeader and ReturnHttpNotAcceptable properties, but they didn't seem to make a difference.
Can anyone see what I've missed to persuade the server to just return the generated HTML?