使用Post/Redirect/Get实现Asp.net防止表单重复提交

来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:48 点击:
前面的Post有提到解决Web中表单重复提交的方法,实际上表单重复提交的问题不单是Asp.net,其它动态Page都有。让我们看下面的图示:

    
然后在刷新页面时经常看到提示框在IE中:

Google Chrome:

Firefox:

 
最简单的解决方法就是使用Post-Redirect-Get模式,就是Http-Post完后,马上做Redirect操作,接下来那个页面是Get。这时用户强制按F5刷新也没有用了。最终实现的效果图:

 
那在Asp.net MVC中如何去做呢,看下面简单View代码:
  

一个包含两个Input的表单:
 <form method="post" id="form1" action="/Security/LoginVerify">
    <p>
       UserName:<input type="text" id="fusername" name="fusername" /><br />
       Password:<input type="password" id="fpassword" name="fpassword" />
       <input type="submit" value="Sign-in" />
    </p>
    </form>
Index Action 在这里做Get的操作, LoginVerify 在这里是Post的目标Action
[HttpPost]
public ActionResult LoginVerify(string fusername, string fpassword)
{
    return this.RedirectToAction("Index", "Security", new { fusername = fusername });
}
public ActionResult Index(string fusername)
{
    ViewBag.UserName = fusername + " login success!";
    return View();
}


对应请求时的HTTP Request RAW是这样的:
POST http://localhost:91/Security/LoginVerify HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://localhost:91/Security/Login
Accept-Language: en-US,zh-CN;q=0.5
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:91
Content-Length: 71
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=qwwlp4rmjnzbsq3ob4dmcg3q
 
Http Response RAW:
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: /Security?fusername=admin
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 24 Mar 2012 02:54:26 GMT
Content-Length: 142
<html><head><title>Object moved</title></head><body>

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • 浅析.NET下XML数据访问新机制
  • asp.net 面试+笔试题目第1/2页
  • C# 邮件地址是否合法的验证
  • asp.net 设置GridView的选中行的实现代码
  • C#高级编程:数据库连接[1]
  • 经典C++程序1
  • IIS 自动回收导致后台定时器失效的问题解决
  • ASP.NET&#160;GridView列表代码示例
  • Asp.net MVC源码分析--Action Filter的链式调用
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1