我无法将视图呈现到字符串然后重定向,尽管2 月的这个答案(我认为是 1.0 版之后)声称这是可能的。我以为我做错了什么,然后我在 7 月读了 Haack 的这个答案,声称这是不可能的。
如果有人让它工作并且可以帮助我让它工作,那就太好了(我会发布代码,错误)。但是,我现在需要解决方法。有一些,但没有什么理想的。有没有人解决这个问题,或者对我的想法有任何意见?
- 这是为了呈现电子邮件。虽然我肯定可以在 Web 请求之外发送电子邮件(将信息存储在数据库中并稍后获取),但电子邮件的类型很多,我不想存储模板数据(用户对象、其他一些 LINQ 对象) ) 在数据库中让它稍后呈现。我可以创建一个更简单、可序列化的 POCO 并将其保存在数据库中,但为什么呢?...我只想渲染文本!
- 我可以创建一个新的 RedirectToAction 对象来检查标头是否已发送(无法弄清楚如何执行此操作 - try/catch?),如果是,则构建一个带有元重定向的简单页面,一个 javascript 重定向,还有一个“点击这里”链接。
- 在我的控制器中,我可以记住我是否呈现了一封电子邮件,如果是,则通过显示视图手动执行 #2。
- 我可以在任何潜在的电子邮件呈现之前手动发送重定向标头。然后,我不使用 MVC 基础结构来重定向操作,而是调用 result.end。这似乎最简单,但确实很混乱。
- 还要别的吗?
编辑:我已经尝试过 Dan 的代码(与我已经尝试过的一月/二月的代码非常相似),但我仍然遇到同样的错误。我能看到的唯一实质性区别是他的示例使用了视图,而我使用了局部视图。稍后我将尝试使用视图对此进行测试。
这是我所拥有的:
控制器
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();
}
发送电子邮件()
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
谢谢,詹姆斯