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

c# – RedirectToAction Bug?

发布时间:2020-12-15 23:51:23 所属栏目:百科 来源:网络整理
导读:我有以下代码: 控制器: public ActionResult Step1(){ return View();}[AcceptVerbs(HttpVerbs.Post)]public ActionResult Step1(FormCollection form){ TempData["messageStatus"] = new Random().Next(1,1000); return RedirectToAction("Step1");} 视图
我有以下代码:

控制器:

public ActionResult Step1()
{
        return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Step1(FormCollection form)
{
        TempData["messageStatus"] = new Random().Next(1,1000);
        return RedirectToAction("Step1");
}

视图:

<%=TempData["messageStatus"]%>

在第一次看到我得到12345例如,但当我请求第二次当然我必须得到别的而不是12345例如54321但不是,我得到相同的结果12345,你怎么解释它? RedirectToAction缓存页面?

这意味着我必须将Guid放入我的网址以解决缓存问题?你怎么看待这个问题?

解决方法

我猜你正在遇到缓存问题.重定向到操作不是问题.所有RedirectToAction都会向浏览器发出重定向响应,告诉它请求Step01.然后您的浏览器发出Step01请求.

在这种情况下,您的浏览器可能会缓存Step01.因此,您需要在STep01中添加一个Response头,表明它永远不应该被缓存.你可以这样做:

[OutputCache(Location = OutputCacheLocation.None)]
public ActionResult Step1()
{
        return View();
}

或者,您可以通过传入任意值将随机查询字符串添加到重定向到操作调用.

(编辑:李大同)

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

    推荐文章
      热点阅读