例外的BinaryWrite"当一个自定义的TextWriter使用&QUOT的OutputStream不可用;在MVC 2 ASP.NET 4 [英] BinaryWrite exception "OutputStream is not available when a custom TextWriter is used" in MVC 2 ASP.NET 4

查看:629
本文介绍了例外的BinaryWrite"当一个自定义的TextWriter使用&QUOT的OutputStream不可用;在MVC 2 ASP.NET 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有渲染使用响应的BinaryWrite法流的视图。这一切都工作得很好ASP.NET 4下使用Beta 2中,但引发此异常的RC版本:


  

HttpException,OutputStream的不
  如果可用自定义的TextWriter是
  用过的。
  


 <%@页标题=LANGUAGE =C#继承=System.Web.Mvc.ViewPage%GT;
<%@导入命名空间=System.IO%GT;
<脚本=服务器>
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(计算机[错误] == NULL)
    {        将Response.Buffer =真;
        Response.Clear();
        Response.ContentType =计算机[的DocType]作为字符串;
        Response.AddHeader(内容处置,计算机[处置]作为字符串);
        Response.CacheControl =无缓存;
        MemoryStream的流=计算机[DocAsStream]作为MemoryStream的;
        Response.BinaryWrite(stream.ToArray());
        Response.Flush();
        Response.Close();
    }
}
< / SCRIPT>
< / SCRIPT>

该视图从客户端重定向产生(jQuery的使用Url.Action助手来解析课程的链接替换previous页面位置调用)。这是所有的iframe中。

任何人都有一个想法,为什么发生这种情况?


解决方案

在的ViewPage开始执行,它假定对请求的其余部分某些事情。被绊倒您的具体的事情是,一个的ViewPage假定请求的其余部分将是一个普通的HTML或其他一些文字的反应,所以它切换响应的的TextWriter有自己的作家。

在你的情况,你应该做一个新的ActionResult派生类,它的ExecuteReuslt方法封装逻辑在Page_Load方法。您的操作方法应该返回您的自定义类的一个实例,并调用运行在适当的时候的ExecuteReuslt方法。这绕过视图引擎完全,其中prevents您正在运行的错误,并给你一个轻微的性能提升。

I have a view rendering a stream using the response BinaryWrite method. This all worked fine under ASP.NET 4 using the Beta 2 but throws this exception in the RC release:

"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>

The view is generated from a client side redirect (jquery replace location call in the previous page using Url.Action helper to render the link of course). This is all in an iframe.

Anyone have an idea why this occurs?

解决方案

When a ViewPage begins executing, it assumes certain things about the remainder of the request. The particular thing that is tripping you up is that a ViewPage assumes that the remainder of the request will be a normal HTML or some other textual response, so it switches the response's TextWriter with its own writer.

In your case, you should make a new ActionResult-derived class whose ExecuteResult method encapsulates the logic in your Page_Load method. Your action method should return an instance of your custom class, and the invoker will run the ExecuteResult method at the appropriate time. This bypasses the view engines entirely, which prevents the error you're running in to and gives you a slight performance boost.

这篇关于例外的BinaryWrite&QUOT;当一个自定义的TextWriter使用&QUOT的OutputStream不可用;在MVC 2 ASP.NET 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆