渲染视图为一个字符串MVC中,然后重定向 - 解决方法? [英] Rendering a view to a string in MVC, then redirecting -- workarounds?

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

问题描述

我无法呈现视图为一个字符串,然后重定向,尽管这个答案从声称二月(1.0版本之后,我认为)这是可能的。我以为我做错了什么,然后我读这<一个href=\"http://stackoverflow.com/questions/1149068/render-view-to-string-followed-by-redirect-results-in-exception\">answer从哈克在声称这是不可能的七月。

I can't render a view to a string and then redirect, despite this answer from Feb (after version 1.0, I think) that claims it's possible. I thought I was doing something wrong, and then I read this answer from Haack in July that claims it's not possible.

如果有人有它的工作,可以帮助我得到它的工作,这是伟大的(并且我会后code,错误)。不过,我现在需要在变通的地步。有一些,但没有什么理想。有没有人解决了这个,或者对我的想法有何评论?

If somebody has it working and can help me get it working, that's great (and I'll post code, errors). However, I'm now at the point of needing workarounds. There are a few, but nothing ideal. Has anybody solved this, or have any comments on my ideas?


  1. 这是渲染电子邮件。虽然我可以肯定发送Web请求的电子邮件外(存储信息的数据库,后来得到它),有很多类型的电子邮件,我不希望存储模板数据(用户对象,一些其他的LINQ对象)在分贝让它以后呈现。我可以创建一个简单的,序列化的POCO并保存在数据库中,但是为什么呢? ......我只是想呈现的文本!

  2. 我可以创建一个检查,如果头已发送(无法弄清楚如何做到这一点? - try / catch语句)的新RedirectToAction对象,如果是这样,建立了一个简单的页面与元重定向, JavaScript重定向,也是一个点击这里链接。

  3. 在我的控制器,我还记得,如果我提供的电子邮件,如果是这样,做手工#2通过显示的视图。

  4. 我可以手动任何潜在的邮件渲染之前发送重定向报头。然后,而不是使用MVC基础设施redirecttoaction,我只是叫result.end。这似乎是最简单的,但真的很乱。

  5. 还有别的吗?

修改:我试过丹的code(非常类似于从一月/二月的code,我已经尝试过),我仍然得到同样的错误。唯一的实质性的区别我可以看到的是,他的例子使用了一个观点,而我使用的局部视图。我会尝试测试稍后以期。

EDIT: I've tried Dan's code (very similar to the code from Jan/Feb that I've already tried) and I'm still getting the same error. The only substantial difference I can see is that his example uses a view while I use a partial view. I'll try testing this later with a view.

下面是我得到了什么:

控制器

public ActionResult Certifications(string email_intro)
        {
            //a lot of stuff

            ViewData["users"] = users;

            if (isPost())
            {
                //create the viewmodel
                var view_model = new ViewModels.Emails.Certifications.Open(userContext)
                {
                    emailIntro = email_intro
                };

                //i've tried stopping this after just one iteration, in case the problem is due to calling it multiple times
                foreach (var user in users)
                {
                    if (user.Email_Address.IsValidEmailAddress())
                    {
                        //add more stuff to the view model specific to this user
                        view_model.user = user;
                        view_model.certification302Summary.subProcessesOwner = new SubProcess_Certifications(RecordUpdating.Role.Owner, null, null, user.User_ID, repository);
                        //more here....

                        //if i comment out the next line, everything works ok
                        SendEmail(view_model, this.ControllerContext);
                    }
                }

                return RedirectToAction("Certifications");
            }

            return View();
        }

SendEmail()

   public static void SendEmail(ViewModels.Emails.Certifications.Open model, ControllerContext context)
        {
            var vd = context.Controller.ViewData;
            vd["model"] = model;
            var renderer = new CustomRenderers();
            //i fixed an error in your code here
            var text = renderer.RenderViewToString3(context, "~/Views/Emails/Certifications/Open.ascx", "", vd, null);
            var a = text;
        }

CustomRenderers

public class CustomRenderers
    {
        public virtual string RenderViewToString3(ControllerContext controllerContext, string viewPath, string masterPath, ViewDataDictionary viewData, TempDataDictionary tempData)
        {
            //copy/paste of dan's code
        }
    }

错误

[HttpException (0x80004005): Cannot redirect after HTTP headers have been sent.]
   System.Web.HttpResponse.Redirect(String url, Boolean endResponse) +8707691

谢谢,
詹姆斯

Thanks, James

推荐答案

更新。

现在,我明白,你要使用的视图引擎来生成HTML实际的电子邮件,我提出以下建议:

Now that I understand that you want to use the view engine to generate the actual email in html, I propose the following:

code渲染动作控制器中的文本:
http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html

Code to render action to text within a controller: http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html

小编辑到code:

public ActionResult Certifications(string email_intro)
{
     //a lot of stuff              
     ViewData["users"] = users;              
     if (isPost())             
     {                 
         //create the viewmodel                 
         var view_model = new ViewModels.Emails.Certifications.Open(userContext) { emailIntro = email_intro };                  

         foreach (var user in users)                 
         {                     
             if (user.Email_Address.IsValidEmailAddress())                     
             {                         
                 view_model.user = user;                         
                 view_model.certification302Summary.subProcessesOwner = new SubProcess_Certifications(RecordUpdating.Role.Owner, null, null, user.User_ID, repository);                         

                 SendEmail(view_model);                     
             }                 
         }                  
         return RedirectToAction("Certifications");             
     }              
     return View();         
 } 

 public void SendEmail(ViewModels.Emails.Certifications.Open model)         
 {             
    var vd = context.Controller.ViewData;             
    vd["model"] = model;             
    var renderer = new CustomRenderers();             

    // Implement the actual email rendering as a regular action method on this controller
    var text = this.CaptureActionHtml(c => (ViewResult)c.RenderEmail(model));

    var a = text;         
} 

这篇关于渲染视图为一个字符串MVC中,然后重定向 - 解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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