2010-09-29 44 views
0

我有一個字符串寫入使用Web客戶端和StringBuilder的一個MSWORD文檔後運行一個頁面重定向。等到代碼執行前運行重定向

 HttpContext.Current.Response.Write(strHTMLContent); 
     this.Page.Response.Redirect("http://www.google.com"); 
     HttpContext.Current.Response.End(); 

但是,由於重定向立即發生,字符串永遠不會寫入(或沒有機會)。

我怎樣才能讓只有等到發生了串寫我重定向運行?

由於球員

這是用於生成的MSWord的代碼:

 HttpContext.Current.Response.Charset = ""; 
     HttpContext.Current.Response.ContentType = "application/msword"; 
     string strFileName = "GenerateDocument" + ".doc"; 
     HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName); 
     StringBuilder strHTMLContent = new StringBuilder(); 
+0

究竟是什麼你想在這裏做什麼?在用戶的瀏覽器中顯示strHTMLContent?或將它們重定向到http://www.google.com/? – Carson63000 2010-09-29 02:40:15

+0

試圖示出的MSWord文檔中strHTMLContent(工作),則瀏覽器重定向至google.com – leonnz 2010-09-29 03:10:36

+0

看到上面的代碼,用於輸出到的MSWord – leonnz 2010-09-29 03:12:10

回答

0

重定向結束所有的處理,並且指示瀏覽器,其中,以指向下一個。你可能想要做的是寫出希望用戶看到的數據,但包括一個meta refresh或維基百科文章中概述的類似機制。

+0

我試圖這樣:HttpContext.Current.Response.Write(strHTMLContent); HtmlMeta meta = new HtmlMeta(); meta.HttpEquiv =「refresh」; meta.Content =「0; url = http://www.google.com/」 this.Header.Controls.Add(meta);但由於某些原因,它不會將元信息添加到我的html,但我可以看到代碼中沒有錯誤。 – leonnz 2010-09-29 02:13:33

+0

@leonnz有沒有辦法,你可以將它包含在你的strHTMLContent的一部分? – 2010-09-29 02:36:27

+0

strHTMLContent中的數據被寫入MSWord文檔,所以元重定向在那裏不太好,因爲我需要瀏覽器窗口來重定向。以編程方式添加元標記沒有奏效......但它應該,還沒有弄清楚爲什麼。 – leonnz 2010-09-29 02:59:22

0

糾正我,如果我錯了,但如果他把它放進一個try/catch /終於刷新不執行響應寫權後,直到?

try 
    { 
     HttpContext.Current.Response.Write(strHTMLContent); 
     HttpContext.Current.Response.End(); 
    } 
catch(Exception ex) 
    { 
     //handle exception 
    } 
finally 
    { 
     this.Page.Response.Redirect("http://www.google.com"); 
    } 
+0

我得到了與此相同的結果,重定向立即運行。整個事情是封裝在一個onclick事件如果重要,我不知道。 – leonnz 2010-09-29 02:15:07