问题描述
在 Asp.net MVC 中:第一次重定向的 TempData 为空,第二次重定向有效,有时效果很好,有时获得第三次重定向
我需要多次刷新页面才能看到 TempData 的有效性
它在本地主机上运行良好,但在服务器上运行不正常。
例如:
在下面这段代码中的“VerifyPaymentView”动作tempdata["VerifyInfo"]第一次为null,需要多次刷新页面才能获取tempdata的内容
public async Task<ActionResult> Verify(int id)
{
var verifyInfo = await VerifyPayment(id,new List<Func<HttpRequestBase,Presenter,PayData,Task<VerifyCallbackData>>>()
{
SetPackage,SetExam,SetQuestionBankId,SetQuestionBankGroupId,SetReportCardLink,VerifySpace
});
if (verifyInfo == null)
{
TempData["Error"] = true;
return RedirectToAction("VerifyPaymentView");
}
TempData["VerifyInfo"] = verifyInfo;
return RedirectToAction("VerifyPaymentView");
}
public ActionResult VerifyPaymentView()
{
var userId = User.GetUserId();
var isLogin = User.Identity.IsAuthenticated;
if (TempData["Error"] != null)
{
TempData["Error"] = true;
return View();
}
if (TempData["VerifyInfo"] != null)
{
var verifyInfo = (VerifyInfo)TempData["VerifyInfo"];
return View(verifyInfo);
}
return null;
}
解决方法
在 VerifyPaymentView 中使用 TempData.Keep() 这将允许您读取值而不将其标记为删除。
@{
TempData.Keep("VerifyInfo");
}