0

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?

1
  • You cannot use an explanation mark. It is a special character. See : en.wikipedia.org/wiki/… Commented May 13, 2019 at 23:34

1 Answer 1

3

How/why are you generating html yourself? I think an eaiser solution might be to make an ASP.NET Core 2 MVC app. That will allow you to use ViewModels. I'd take a look into that.

Anyways, try returning Content... this returns a Http Status code of 200, and will allow you to return a string with other details of how the content is formatted.

[HttpGet]
[Produces("text/html")]
public IActionResult Display()
{
     return Content("<html><h1>hello world!</h1></html>", "text/html", Encoding.UTF8);
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's an unusual requirement, I agree, but definitely better than a more standard approach (which has already been tried) in this case. Thanks - that works :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.