在MVC中将视图呈现为字符串,然后重定向 - 有解决方法吗?

4
尽管这篇2月份的答案(我认为是在1.0版本后)声称可以将视图渲染为字符串然后重定向,但我仍无法实现。我以为是自己操作有误,但随后我看到了7月份Haack的答案,声称不可能实现。
如果有人已经成功实现并能够帮我解决问题,那么太好了(我会贴出代码和错误信息)。然而,我现在需要使用一些变通的方法。虽然有一些方法,但没有最理想的。有没有人解决过这个问题,或者对我的想法有任何评论?
  1. 这是用来渲染电子邮件的。虽然我可以在Web请求之外发送电子邮件(将信息存储在数据库中并稍后获取),但有许多类型的电子邮件,我不想将模板数据(用户对象、几个其他LINQ对象)存储在数据库中以便以后再渲染。我可以创建一个更简单、可序列化的POCO,并将其保存在数据库中,但为什么要这样做呢?...我只想要渲染后的文本!
  2. 我可以创建一个新的RedirectToAction对象,检查头是否已发送(无法弄清楚如何做到这一点--尝试/捕获?),如果是,则构建出一个简单的页面,其中包含meta重定向、javascript重定向以及"点击此处"链接。
  3. 在我的控制器中,我可以记住是否已经渲染了电子邮件,并且如果是,通过显示视图手动执行第2步。
  4. 我可以在任何可能的电子邮件渲染之前手动发送重定向头。然后,不使用MVC基础设施进行重定向操作,而是直接调用result.end。这似乎最简单,但真的很混乱。
  5. 还有其他方法吗?

编辑:我尝试了丹的代码(非常类似于我已经尝试过的1月/2月的代码),但仍然遇到相同的错误。我唯一能看到的实质性差异是他的示例使用视图,而我使用部分视图。稍后我将尝试使用视图进行测试。

以下是我的代码:

控制器

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;
        }

自定义渲染器

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

感谢您,詹姆斯。

你有没有找到解决这个问题的方法? - Kirschstein
3个回答

0
public Action SendEmail(int id)
{
  //Let's say that id is the db id of an order that a customer has just placed.

  //Go get that model from the db.
  MyModel model = new Model(id);

  //Now send that email. Don't forget the model and controller context.
  SendEmail(model, this.ControllerContext);

  //Render (or redirect!)
  return RedirectToAction("Wherever");
}

private static void SendEmail(MyModel model, ControllerContext controllerContext)
{
  //Recreate the viewdata
  ViewDataDictionary viewData = controllerContext.Controller.ViewData;
  viewData["Order"] = model;
  string renderedView = "";
  CustomRenderers customRenderers = new CustomRenderers();

  //Now render the view to string
  //ControllerContext, ViewPath, MasterPath, ViewDataDictionary, TempDataDictionary
  //As you can see, we're not passing a master page, and the tempdata is in this instance.
  renderedView = RenderViewToString(controllerContext, "~/Views/Orders/Email.aspx", "", viewData, null);

  //Now send your email with the string as the body.
  //Not writing that, as the purpose is just to show the rendering. :)
}


//Elsewhere...
public class CustomRenderers
{
  public virtual string RenderViewToString(ControllerContext controllerContext, string viewPath, string masterPath, ViewDataDictionary viewData, TempDataDictionary tempData)
  {
    if (tempData == null)
    {
    tempData = new TempDataDictionary();
    }

    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;
    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;
    }
  }
}

在您的Orders/Email.aspx视图中,确保您从ViewData中引用所有内容,而不是模型。 您可以这样做:
<% MyModel model = (MyModel)ViewData["Order"] %>

谢谢你的尝试,但是我还是遇到了同样的“无法重定向错误”,这个错误在使用你一月份帖子中的代码后仍然存在。我已经将所有内容都粘贴在我的问题中了。我做错了什么?谢谢。 - James S

0

已更新。

现在我明白你想要使用视图引擎来生成实际的HTML邮件,我建议如下:

在控制器中渲染操作为文本的代码: http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html

对你的代码进行小修改:

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;         
} 

你会如何建议我实现我在示例中尝试做的事情?也就是说,我想使用模板引擎来创建电子邮件,而不是将它们和一堆HTML连接在一起。 - James S
谢谢...我会在一个月或两个月后回到那个项目时查看它。 - James S

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接