Back button doesn't cause postback to a controller action in MVC

落爺英雄遲暮 提交于 2021-02-07 07:11:04

问题


When I click the back button in IE10 or Chrome on Win7, it does not hit my break point in my MVC controller. The Network tab in IE developer's tools shows it had a 304 not modified and Fiddler doesn't capture the request.

I was expecting the post back, so I could do work in my controller. In my case, the bug is:

  1. Sign in
  2. make sure you are on the default page
  3. click the browser back button on the top left you'll now be back to the login screen
  4. sign in with your same credentials again when you do that - I get "The provided anti-forgery token was meant for user "", but the current user is "username".

I've tried putting this in my controller, without success:

this.HttpContext.Response.CacheControl = "private";
this.HttpContext.Response.Cache.SetMaxAge(TimeSpan.FromSeconds(0));
public ActionResult Index()
{
    // Get: /Home/Index
    if (this.User.Identity.IsAuthenticated)
    {
        // send the user to the GlobalAssetDashboard
        return this.RedirectToAction(
            "GlobalAssetDashboard",
            "Dashboard",
            new
                {
                    area = "DashboardArea"
                });
    }

    return this.View("Login");
}
 public ActionResult Login()
{
    // GET: /Home/Login
    if (this.User.Identity.IsAuthenticated)
    {
        // send the user to the GlobalAssetList
        return this.RedirectToAction(
            "GlobalAssetDashboard",
            "Dashboard",
            new
                {
                    area = "DashboardArea"
                });
    }

    return this.View("Login", new LoginModel());
}

Is there a way to force the postback or detect this and cause a refresh in JavaScript? Or maybe I have my controller methods implemented incorrectly?


回答1:


Typically caching rules like this aren't conditional upon the logic they perform, the URL as a whole is either cached or it isn't. In which case something as simple as this should suffice.

[OutputCache(NoStore=true, Duration=0)]
public ActionResult Login()
{

}

http://msdn.microsoft.com/en-us/library/dd492556(v=vs.108).aspx



来源:https://stackoverflow.com/questions/16308198/back-button-doesnt-cause-postback-to-a-controller-action-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!