如何处理表单提交ASP.NET MVC后退按钮?

时间:2022-08-28 04:03:44

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.

如果用户再次点击提交,我将获得一些潜在的奇怪错误。

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

那么就asp.net mvc而言,处理点击后退按钮的用户的最佳方法是什么?

thanks!

2 个解决方案

#1


6  

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

此解决方案适用于整个控制器或特定操作,只需添加[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="https://*.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();
    }
}

And in your code:

在你的代码中:

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

Originally posted here: MVC Back button issue

最初发布在这里:MVC后退按钮问题

#2


1  

What do you mean by "potential weird bugs"? I doubt the user would click submit again unless they wanted to post exactly the same thing again. I you don't want duplicate posts, check the content against your DB before posting.

“潜在的奇怪错误”是什么意思?我怀疑用户会再次点击提交,除非他们想再次发布完全相同的内容。我不想要重复的帖子,在发布之前检查数据库中的内容。

If you really don't want people posting the same form twice, put a randomly generated number (just make sure it's random enough to avoid collisions, or use something like a combination of the user ID and a precise timestamp) into a hidden field, save that with your data and check that it doesn't already exist in your DB before saving anything.

如果您真的不希望人们两次发布相同的表单,请将随机生成的数字(只需确保它足够随机以避免冲突,或使用类似用户ID和精确时间戳的组合)放入隐藏字段,保存数据,并在保存任何内容之前检查数据库中是否存在该数据。

#1


6  

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

此解决方案适用于整个控制器或特定操作,只需添加[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="https://*.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();
    }
}

And in your code:

在你的代码中:

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

Originally posted here: MVC Back button issue

最初发布在这里:MVC后退按钮问题

#2


1  

What do you mean by "potential weird bugs"? I doubt the user would click submit again unless they wanted to post exactly the same thing again. I you don't want duplicate posts, check the content against your DB before posting.

“潜在的奇怪错误”是什么意思?我怀疑用户会再次点击提交,除非他们想再次发布完全相同的内容。我不想要重复的帖子,在发布之前检查数据库中的内容。

If you really don't want people posting the same form twice, put a randomly generated number (just make sure it's random enough to avoid collisions, or use something like a combination of the user ID and a precise timestamp) into a hidden field, save that with your data and check that it doesn't already exist in your DB before saving anything.

如果您真的不希望人们两次发布相同的表单,请将随机生成的数字(只需确保它足够随机以避免冲突,或使用类似用户ID和精确时间戳的组合)放入隐藏字段,保存数据,并在保存任何内容之前检查数据库中是否存在该数据。