加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – TempData不适用于MVC4中的第二个请求

发布时间:2020-12-16 06:22:53 所属栏目:asp.Net 来源:网络整理
导读:我以前从未见过这样的事情而且很难过.我有以下控制器序列: /// summary /// Helper method to store offerId to TempData /// /summary /// param name="offerId"/param private void StoreOfferInTempData(string offerId) { if (TempData.ContainsKey(Sel
我以前从未见过这样的事情而且很难过.我有以下控制器序列:

/// <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,所以我认为没有办法以某种方式将值标记为删除.此外,它在Payment方法中是否有[AllowAnonymous]属性失败(因此不是由于任何http到https开关或类似的东西).

似乎它必须是非常简单的东西,但我的搜索没有任何结果.任何帮助非常感谢.

更新:在进一步检查时,由于某种原因,似乎我的整个背景都在这一步骤上.我们在控制器中使用IoC,但没有IoC实例化的项目.神秘感加深了.

解决方法

TempData的生命周期只有在它被读回或下一个请求被处理之后才会发生(它始终是第一个).如果您要处理两个(或三个)请求,则不应依赖TempData.而是使用会话或数据库.

TempData的目的是在请求之前切换信息,直到您清除它为止(这是会话的目的).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读