c# - 尝试在 ActionResult 中返回 HTML 会导致 HTTP 406 错误

标签 c# asp.net-mvc iframe asp.net-core-mvc

我试图在 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/

相关文章:

c# - 将 Microsoft Office Communicator 2007 集成到 ASP.NET 页面中

javascript - 具有动态内容的动态附加 iframe 在内容元素上写入 jquery 事件句柄不起作用

jquery - 使用 Jquery 将复杂类型发布到 Asp.Net MVC

javascript - 打开每个 iframe,每个 iframe 都有单独的按钮

javascript - 跨域 iframe - 更改复选框状态

c# - 如何在 Active Directory C# 中的指定 ou 下面列出 ous(组织单位)

c# - 动态编译以提高性能

c# - 如何将 C++ 类转换为 .NET 对象?

jquery - 使用 jquery 使图像在 div 中可见

jquery - ASP.NET MVC 使用 JQuery 将页面内容加载到 div 中