我试图在 ActionResult
中返回 HTML .我已经试过了:
[Produces("text/html")]
public ActionResult DisplayWebPage()
{
return Content("<html><p><i>Hello! You are trying to view <u>something!</u></i></p></html>");
}
这在 <iframe>
中不显示任何内容.我试过:
[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 给我以下消息:
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 和 Chrome 拒绝显示任何内容。我也试过HtmlEncode
和正常的 ActionResult
.这是我的 <iframe>
的片段在我看来:
<div class="row">
<div class="col-sm-12">
<iframe src="/Home/DisplayWebPage" class="col-sm-12"></iframe>
</div>
</div>
为什么我没有收到任何结果?我做错了什么吗?
最佳答案
Produces("text/html")
不会有任何效果,因为没有内置的 HTML 输出格式化程序。
要解决您的问题,只需明确指定内容类型:
public ActionResult DisplayWebPage()
{
return Content("<html><p><i>Hello! You are trying to view <u>something!</u></i></p></html>", "text/html");
}
另一种选择是将操作的返回类型更改为 string
并通过 Accept
header 请求 text/html
格式。参见 Introduction to formatting response了解详情。
关于c# - 尝试在 ActionResult 中返回 HTML 会导致 HTTP 406 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47605746/