Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to return HTML in an ActionResult results in a HTTP 406 Error

I am trying to return HTML in an ActionResult. I have already tried:

[Produces("text/html")]
public ActionResult DisplayWebPage()
{
    return Content("<html><p><i>Hello! You are trying to view <u>something!</u></i></p></html>");
}

This displays nothing in the <iframe>. I have tried:

[Produces("text/html")]
public string DisplayWebPage()
{
    return HttpUtility.HtmlDecode("<html><p><i>Hello! You are trying to view <u>something!</u></i></p></html>");
}

Microsoft Edge gives me the following message:

HTTP 406 error
This page isn’t speaking our language Microsoft Edge can’t display this page because it wasn’t in a format that can be shown.

Firefox and Chrome refuse to display anything. I have also tried HtmlEncode and the normal ActionResult. Here is the segment for my <iframe> in my View:

<div class="row">
    <div class="col-sm-12">
        <iframe src="/Home/DisplayWebPage" class="col-sm-12"></iframe>
    </div>
</div>

Why am I not receiving any results? Am I doing something wrong?

like image 754
Razor Avatar asked Dec 02 '17 07:12

Razor


1 Answers

Produces("text/html") will not have any effect because there is no built-in output formatter for HTML.

To fix your problem just specify content type explicitly:

public ActionResult DisplayWebPage()
{
    return Content("<html><p><i>Hello! You are trying to view <u>something!</u></i></p></html>", "text/html");
}

Another option is to change return type of your action to string and request text/html format via Accept header. See Introduction to formatting response for the details.

like image 125
CodeFuller Avatar answered Sep 29 '22 18:09

CodeFuller