mel*_*aos 5 asp.net asp.net-mvc back-button
我有一个表单,允许用户键入数据,然后提交.如果一切都在这个动作结果上运行良好,那么我会将用户重定向回感谢页面.
我现在的问题是,当用户点击后退按钮时,他们将能够返回到表单页面,输入仍然存在.
如果用户再次点击提交,我将获得一些潜在的奇怪错误.
那么就asp.net mvc而言,处理点击后退按钮的用户的最佳方法是什么?
谢谢!
此解决方案适用于整个控制器或特定操作,只需添加[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();
}
}
Run Code Online (Sandbox Code Playgroud)
在你的代码中:
[NoCache]
[Authorize]
public class AccountController : Controller
{ ... }
Run Code Online (Sandbox Code Playgroud)
最初发布在这里:MVC后退按钮问题
“潜在的奇怪错误”是什么意思?我怀疑用户会再次单击“提交”,除非他们想再次发布完全相同的内容。如果您不想要重复的帖子,请在发布之前根据您的数据库检查内容。
如果您确实不希望人们两次发布相同的表单,请将随机生成的数字(只需确保它足够随机以避免冲突,或使用用户 ID 和精确时间戳的组合)到隐藏字段中,将其与您的数据一起保存,并在保存任何内容之前检查它是否已存在于您的数据库中。
| 归档时间: |
|
| 查看次数: |
6307 次 |
| 最近记录: |