渲染视图的字符串,然后在例外重定向结果 [英] Render view to string followed by redirect results in exception

查看:220
本文介绍了渲染视图的字符串,然后在例外重定向结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里的问题:我建立电子邮件要由我的应用程序通过渲染众目睽睽页面为字符串并将它们发送发送。因为我不是重定向到另一个网址的网站上以后这工作没有任何问题这么久。每当我尝试,我得到System.Web.HttpException:后HTTP标头已被送往不能重定向。

我相信这个问题来自我重用从控制器动作,其中呼吁创建电子邮件来自于环境的事实。更具体地,从上下文中的Htt presponse。不幸的是,我不能创建一个新的Htt presponse,使得使用的HttpWriter因为类的构造函数是不可到达,并使用自TextWriter派生其他类导致response.Flush()抛出一个异常,本身。

有没有人有办法解决这个?

 公共静态字符串RenderViewToString(
        ControllerContext controllerContext,
        串viewPath,
        串masterPath,
        可视数据的ViewDataDictionary,
        TempDataDictionary的TempData)
    {
        流过滤器= NULL;
        的ViewPage的ViewPage =新的ViewPage();        //对,共创我们的观点
        viewPage.ViewContext =新ViewContext(controllerContext,
            新WebFormView(viewPath,masterPath),可视数据,TempData的);        //获取响应的背景下,进行冲洗并得到响应滤波器。
        VAR响应= viewPage.ViewContext.HttpContext.Response;
        // VAR响应=新的Htt presponseWrapper(新的Htt presponse
        //(**的TextWriter放这里**));
        response.Flush();
        VAR oldFilter = response.Filter;        尝试
        {
            //把新的过滤器到响应
            过滤器=新的MemoryStream();
            response.Filter =滤波器;            //现在渲染视图到的MemoryStream并刷新响应
            viewPage.ViewContext.View.Render(viewPage.ViewContext,
                viewPage.ViewContext.HttpContext.Response.Output);
            response.Flush();            //现在读渲染视图。
            filter.Position = 0;
            VAR读者=新的StreamReader(过滤器,response.ContentEncoding);
            返回reader.ReadToEnd();
        }
        最后
        {
            //清理。
            如果(过滤器!= NULL)
                filter.Dispose();            //现在更换响应滤波器
            response.Filter = oldFilter;
        }
    }


解决方案

您不得不启动新的请求。
位,你真的要发送电子邮件同步这种方式?如果邮件服务器关闭时,用户可能会等待了好一阵。

我总是把电子邮件脱机队列,并有一个服务邮箱当中。您可以考虑使用此星火模板引擎。

另外一种方法是不定向但与元重定向代码写出来的网页

So here's the issue: I'm building e-mails to be sent by my application by rendering full view pages to strings and sending them. This works without any problem so long as I'm not redirecting to another URL on the site afterwards. Whenever I try, I get "System.Web.HttpException: Cannot redirect after HTTP headers have been sent."

I believe the problem comes from the fact I'm reusing the context from the controller action where the call for creating the e-mail comes from. More specifically, the HttpResponse from the context. Unfortunately, I can't create a new HttpResponse that makes use of HttpWriter because the constructor of that class is unreachable, and using any other class derived from TextWriter causes response.Flush() to throw an exception, itself.

Does anyone have a solution for this?

    public static string RenderViewToString(
        ControllerContext controllerContext,
        string viewPath,
        string masterPath,
        ViewDataDictionary viewData,
        TempDataDictionary tempData)
    {
        Stream filter = null;
        ViewPage viewPage = new ViewPage();

        //Right, create our view
        viewPage.ViewContext = new ViewContext(controllerContext,
            new WebFormView(viewPath, masterPath), viewData, tempData);

        //Get the response context, flush it and get the response filter.
        var response = viewPage.ViewContext.HttpContext.Response;
        //var response = new HttpResponseWrapper(new HttpResponse
        //    (**TextWriter Goes Here**));
        response.Flush();
        var oldFilter = response.Filter;

        try
        {
            //Put a new filter into the response
            filter = new MemoryStream();
            response.Filter = filter;

            //Now render the view into the memorystream and flush the response
            viewPage.ViewContext.View.Render(viewPage.ViewContext,
                viewPage.ViewContext.HttpContext.Response.Output);
            response.Flush();

            //Now read the rendered view.
            filter.Position = 0;
            var reader = new StreamReader(filter, response.ContentEncoding);
            return reader.ReadToEnd();
        }
        finally
        {
            //Clean up.
            if (filter != null)
                filter.Dispose();

            //Now replace the response filter
            response.Filter = oldFilter;
        }
    }

解决方案

You'd have to initiate a new request. Bit, do you really want to send emails synchronously this way? If the mail server is down, the user could be waiting a good while.

I always put emails in an offline queue and have a service mail them. You might consider using the Spark template engine for this.

One other approach is to not redirect but write out a page with a meta redirect tag

这篇关于渲染视图的字符串,然后在例外重定向结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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