2015-03-31 49 views
0

不保留值我有以下代碼:的TempData在FileContentResult

[Authorize] 
    [HttpGet] 
    public FileContentResult DownloadMsgAsPdf(string FileId, string outer = "") 
    { 
     var Results = (SearchResult)TempData["Results"]; 
     var msg = Results.emails[FileId]; 
... 
     return File(pdfBytes, System.Net.Mime.MediaTypeNames.Application.Octet, msg.Id + ".pdf"); 
    } 

現在在運行它一次(點擊鏈接),但這一切完全沒問題。然而,一旦第二次點擊它(沒有其他改變或調用),它就會出錯,結果爲空。我認爲這意味着TempData正在清除,但這怎麼可能?

以下是完整的代碼,如果需要這種耐心:

[Authorize] 
    [HttpGet] 
    public FileContentResult DownloadMsgAsPdf(string FileId, string outer = "") 
    { 
     var Results = (SearchResult)TempData["Results"]; 
     var msg = Results.emails[FileId]; 

     TempData.Keep(); 

     msg.BodyHTML = System.Web.HttpUtility.HtmlDecode(msg.BodyHTML); 

     var htmlContent = String.Format("<center><p><b>Case ID</b>: " + msg.Id 
     + " <b>User ID</b>: " + Session["userid"] 
     + " <b>Privileged</b>: "); 
     if (msg.Tags != null) 
     { 
      htmlContent += msg.Tags.ContainsKey("privileged"); 
     } 
     else 
     { 
      htmlContent += "False"; 
     } 
     htmlContent += " <b>Downloaded</b>: " + DateTime.Now.ToString("g") + "</center>" 
     + " <p> -- Forwarded message -- </p> " 
     + " <p><b> From :</b> <a href='#'>" + msg.From + "</a> </p> " 
     + " <p><b> Date:</b> Mon, oct 25 2010 at 4:42 PM </p> " 
     + " <p> <b>Subject:</b> " + msg.Subject + " </p> " 
     + " <p><b> To:</b> " + msg.To + " </p> " 
     + " <p><b> Cc: </b> </p> " 
     + " <div> " 
     + " <ul> "; 
     if(msg.attachementList != null){ 
      if (!msg.attachementList.Any()) 
      { 
       htmlContent += "<text> " 
       + "<li><label style='color:gray'>No attachment</label></li></text>"; 
      } 
      else 
      { 
       foreach (var attachment in msg.attachementList) 
       { 
        var filename = System.IO.Path.GetFileName(attachment); 
        htmlContent += "<text> " 
        + "<li><a href='#'>" + filename + "</a></li></text>"; 
       } 
      } 
     } 
     htmlContent += "</ul>" 
     + "</div>" 
     + "<div padding: 5px;'>" 
     + msg.BodyText 
     + "</div>" 
     + "</div>"; 

     var pdfBytes = (new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(htmlContent); 

     if (outer != "") 
     { 
      return File(pdfBytes, System.Net.Mime.MediaTypeNames.Application.Octet, msg.Id + ".pdf"); 
     } 

     return new FileContentResult(pdfBytes, "application/pdf"); 
    } 

回答

0

TempData意味着你只能獲得一次。我看到你使用TempData.Keep(),但這種方法隻影響相同的http上下文。我想你可以使用SessionCache而不是TempData

+0

感謝您的回覆,有什麼方法可以多次使用嗎? 我試過Session並得到了同樣的錯誤。 – user2463732 2015-03-31 03:58:53