儘管this answer from Feb(我認爲版本1.0之後)聲稱它是可能的,但我無法將視圖渲染爲字符串,然後重定向。我以爲我做錯了什麼,然後我讀了這個answer from Haack in July,聲稱這是不可能的。將視圖渲染爲MVC中的字符串,然後重定向 - 解決方法?
如果有人有它的工作,並可以幫助我得到它的工作,這是偉大的(我會張貼代碼,錯誤)。不過,我現在需要解決方法。有一些,但沒有理想。有沒有人解決這個問題,或者對我的想法有任何評論?
- 這是呈現電子郵件。儘管我可以確實在Web請求外發送電子郵件(將信息存儲在數據庫中並在稍後獲取),但有很多類型的電子郵件,我不想存儲模板數據(用戶對象,其他一些LINQ對象)在一個分貝,讓它得到渲染。我可以創建一個更簡單的,可序列化的POCO並將其保存在數據庫中,但爲什麼? ...我只想渲染文本!
- 我可以創建一個新的RedirectToAction對象來檢查頭是否已經發送(不知道如何做到這一點 - 嘗試/ catch?),如果是這樣,用meta重定向構建一個簡單的頁面,一個JavaScript重定向,還有一個「點擊這裏」鏈接。
- 在我的控制器中,我可以記得我是否已經提交了一封電子郵件,如果是,則通過顯示一個視圖手動執行#2。
- 我可以在任何潛在的電子郵件呈現之前手動發送重定向標頭。然後,我只是調用result.end而不是使用MVC基礎結構重定向。這看起來最簡單,但真的很混亂。
- 還有什麼?
編輯:我試過丹的代碼(非常相似,從一月/二月,我已經嘗試過的代碼),我仍然得到同樣的錯誤。我能看到的唯一實質性區別是他的示例在使用局部視圖時使用視圖。我會稍後嘗試使用視圖進行測試。
下面是我得到了什麼:
控制器
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
謝謝, 詹姆斯
您是否曾經爲此找到過解決方法? – Kirschstein 2009-11-25 11:57:58