如何处理表单提交ASP.NET MVC后退按钮? [英] How to handle form submission ASP.NET MVC Back button?

查看:132
本文介绍了如何处理表单提交ASP.NET MVC后退按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形式,它允许用户在数据键,然后提交。
如果一切正常的这一行动结果,那么我将用户重定向到一个感谢您网页。

i have a form which allows the user to key in the data and then submit. if everything works well on this action result, then i will redirect the user back to a thank you page.

我的问题,现在是当上的后退按钮,用户点击,他们将能够回到表单页面,并输入依然存在。

my problem right now is that when the user click on the back button, they will be able to go back to the form page and the inputs will still be there.

如果用户只需点击提交再次,我会得到一些潜在的莫名其妙的错误。

and if the user just click on submit again, i will be getting some potential weird bugs.

所以在asp.net mvc的方面,有什么处理谁的后退按钮单击?用户的最佳方式。

so in terms of asp.net mvc, what's the best way to handle users who click on the back button?

谢谢!

推荐答案

该解决方案完全适用,那么整个控制器或一个特定的动作,只需添加[NOCACHE]

This solution works perfectly for both the whole controller or a specific action, simply add [NoCache]

 /// <summary>
 /// Prevent a controller or specific action from being cached in the web browser.
 /// For example - sign in, go to a secure page, sign out, click the back button.
 /// <seealso cref="http://stackoverflow.com/questions/6656476/mvc-back-button-issue/6656539#6656539"/>
 /// </summary>
public class NoCacheAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var response = filterContext.HttpContext.Response;
        response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        response.Cache.SetValidUntilExpires(false);
        response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        response.Cache.SetCacheability(HttpCacheability.NoCache);
        response.Cache.SetNoStore();
    }
}

而在你的code:

And in your code:

[NoCache]
[Authorize]
public class AccountController : Controller
{ ... }

最初张贴在这里: MVC后退按钮问题

这篇关于如何处理表单提交ASP.NET MVC后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆