我以前从未见过这种情况,我很困惑。我有以下控制器序列:
/// <summary>
/// Helper method to store offerId to TempData
/// </summary>
/// <param name="offerId"></param>
private void StoreOfferInTempData(string offerId)
{
if (TempData.ContainsKey(SelectedOfferKey))
TempData.Remove(SelectedOfferKey);
TempData.Add(SelectedOfferKey, offerId);
}
[HttpPost]
[AllowAnonymous]
public virtual ActionResult Step1(MyViewModel model)
{
if (ModelState.IsValid)
{
StoreOfferInTempData(model.SelectedOfferId);
return RedirectToAction(MVC.Subscription.Register());
}
MySecondViewModel model2 = new MySecondViewModel { OfferId = model.SelectedOfferId };
return View(model2);
}
[HttpGet]
[AllowAnonymous]
public virtual ActionResult Register()
{
string offerId = TempData[SelectedOfferKey] as string; //we get a valid value here
... error handling content elided ...
RegisterViewModel model = new RegisterViewModel { OfferId = offerId };
return View(model);
}
[HttpPost]
[AllowAnonymous]
public virtual ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
CreateCustomerResult result = CustomerService.CreateAccount(model.Email, model.NewPassword);
if (result.Success)
{
... content elided; storing customer to Session ...
MyMembershipProvider.PersistUserCookie(result.Principal, true);
//need to store our selected OfferId again for use by the next step
StoreOfferInTempData(model.OfferId);
return RedirectToAction(MVC.Subscription.Payment());
}
model.ErrorMessage = result.ErrorMessage;
}
return View(model);
}
[HttpGet]
public ActionResult Payment()
{
string offerId = TempData[SelectedOfferKey] as string; //this is null???
... content elided ...
return View(model);
}
TempData 的第一轮存储按预期运行。该值存在于随后的 HttpGet 方法中,并被标记为删除,因此当我再次添加它时它不再存在。但是,在第三个 HttpGet 方法上,它返回 null。
我尝试过为每一轮使用不同的密钥,没有任何变化。我可以向您保证,除了显示的内容外,我不会检查 TempData,因此我看不出该值会以某种方式被标记为删除。此外,无论它是否具有 [AllowAnonymous] 属性,它都会在支付方法中失败(所以不是由于任何 http 到 https 切换或类似的东西。
似乎它必须是非常简单的东西,但我的搜索没有任何结果。非常感谢任何帮助。
更新:在进一步检查中,出于某种原因,我的整个上下文似乎都在这一步上被淹没了。我们在控制器中使用 IoC,但没有任何 IoC 实例化的项目。谜团加深。