gpt4 book ai didi

asp.net-mvc-2 - MVC 2 ASP.NET 4 中的 BinaryWrite 异常 "OutputStream is not available when a custom TextWriter is used"

转载 作者:行者123 更新时间:2023-12-02 14:33:09 24 4
gpt4 key购买 nike

我有一个使用响应 BinaryWrite 方法呈现流的 View 。这一切在使用 Beta 2 的 ASP.NET 4 下运行良好,但在 RC 版本中抛出此异常:

"HttpException" , "OutputStream is not available when a custom TextWriter is used."

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (ViewData["Error"] == null)
{

Response.Buffer = true;
Response.Clear();
Response.ContentType = ViewData["DocType"] as string;
Response.AddHeader("content-disposition", ViewData["Disposition"] as string);
Response.CacheControl = "No-cache";
MemoryStream stream = ViewData["DocAsStream"] as MemoryStream;
Response.BinaryWrite(stream.ToArray());
Response.Flush();
Response.Close();
}
}
</script>


</script>

View 是从客户端重定向生成的(jquery 替换上一页中的位置调用,当然使用 Url.Action 帮助器来呈现链接)。这一切都在 iframe 中。

有人知道为什么会发生这种情况吗?

最佳答案

当 ViewPage 开始执行时,它会假设有关请求的其余部分的某些事情。让您困惑的具体事情是 ViewPage 假定请求的其余部分将是普通的 HTML 或其他文本响应,因此它用自己的编写器切换响应的 TextWriter。

在您的情况下,您应该创建一个新的 ActionResult 派生类,其 ExecuteResult 方法将逻辑封装在 Page_Load 方法中。您的操作方法应返回自定义类的实例,并且调用程序将在适当的时间运行 ExecuteResult 方法。这完全绕过了 View 引擎,从而防止了您遇到的错误,并为您带来了轻微的性能提升。

关于asp.net-mvc-2 - MVC 2 ASP.NET 4 中的 BinaryWrite 异常 "OutputStream is not available when a custom TextWriter is used",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2261198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com