我有以下代码:
[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");
}
现在运行一次(点击一个链接)它完全可以。但是,在第二次单击它时(没有其他任何更改或调用),它会出错,结果为null。我认为这意味着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 :(得分:0)
TempData
意味着你只能获得一次。我看到你使用TempData.Keep()
,但这种方法只影响同一个http上下文。我认为您可以使用Session
或Cache
代替TempData
。