【问题标题】:back button of browser does not return the desired view and returns a view in cache instead浏览器的后退按钮不返回所需的视图,而是返回缓存中的视图
【发布时间】:2019-04-21 20:43:01
【问题描述】:

我有一个注册页面,用户可以在其中注册,注册后,他的令牌将被存储。然后注册后我会将他重定向到商店页面。如果他已经注册并且他点击了浏览器的返回按钮,他应该被重定向到主页(并且不再看到注册页面)。但是现在使用我当前的代码,当用户在注册后按下后退按钮时,他会回到注册页面!有任何想法吗?这是我的控制器,如果他已登录并注册,则返回主页视图:

public virtual ActionResult Register(string returnUrl, string invitationCode = null,
            string emailAddressOrMobileNumber = null)
        {
            if (Request.IsAuthenticated) return RedirectToAction(MVC.Home.Index());}

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    您可以在Home:Index 操作中设置这些代码以清除缓存控制。您还需要检查UrlReferrer 以确认通过注册页面返回主页。

    if (Request.UrlReferrer.PathAndQuery == "/register")
    {
        HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("Expires", "0");
    }
    

    【讨论】:

      【解决方案2】:

      如果你只返回一个视图,你应该:
      1) 将 html 部分或 DIV 添加到您的视图中或介绍您的布局并设置 css 属性以将此 DIV 放在页面底部,例如:

      HTML:

      <div id="bottom" > </div>
      

      CSS:

       #bottom { 
              height: 40px; 
              position: fixed; 
              bottom:0%;
              width:100%; 
              background-color: #393838; 
              opacity: 1;
          }
      

      2) 在返回动作中,你应该返回你的路由 url + 你的 DIV 的 id 以滚动到这个 div
      Controller :

      public virtual ActionResult Register(string returnUrl, string invitationCode = null,
                  string emailAddressOrMobileNumber = null)
              {
                  if (Request.IsAuthenticated) return RedirectToAction(MVC.Home.Index());
      
      }
      return Redirect(Url.RouteUrl(new { controller = "Controller", action = "Action" }) + "#bottom");
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-09
        • 2021-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-19
        • 2011-07-11
        • 2016-05-14
        相关资源
        最近更新 更多