5

我有一个表单,允许用户输入数据然后提交。如果此操作结果一切正常,那么我会将用户重定向回感谢页面。

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

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

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

谢谢!

4

3 回答 3

9

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

在您的代码中:

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

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

于 2015-06-21T11:30:09.717 回答
1

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

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

于 2010-04-13T08:58:57.550 回答
0

使用此代码

@using (Html.BeginForm("FindResults", "COntrollerName", FormMethod.Get, new { id = "SearchForm" }))
{}
public ActionResult Index()
    {
return view();
}
[HttpGet]
    public ActionResult FindResults(FindIssueModel model)
    {
}
于 2019-01-15T10:27:00.770 回答