博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Post/Redirect/Get实现Asp.net防止表单重复提交
阅读量:4597 次
发布时间:2019-06-09

本文共 3308 字,大约阅读时间需要 11 分钟。

      前面的Post有提到解决Web中表单重复提交的方法,实际上表单重复提交的问题不单是Asp.net,其它动态Page都有。让我们看下面的图示:

    

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

Google Chrome:

Firefox:

 

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

 

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

一个包含两个Input的表单:

UserName:

Password:

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/1.1

Accept: text/html, application/xhtml+xml, */*
Referer:
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>

<h2>Object moved to <a href="/Security?fusername=admin">here</a>.</h2>
</body></html>

在现在大多数的Web应用程序中都使用是Http 302的重定向。Http 1.1说明书中引用就是用来应对这种用户提交表单后可以在浏览器安全的刷新场景。 HTTP 303 意义是这样的:

Used to tell the client that the resource should be fetched using a different URL. This

new URL is in the Location header of the response message. Its main purpose is to
allow responses to POST requests to direct a client to a resource.

 

在Asp.net MVC可以这些去实现一个自定义ActionResult:

/// /// SeeOtherRedirectResult/// public class SeeOtherRedirectResult : ActionResult{    private string _url;    ///     /// Initializes a new instance of the 
class. ///
/// Target URL. public SeeOtherRedirectResult(string url) { _url = url; } /// /// Enables processing of the result of an action method by a custom type that inherits from the
class. ///
/// The context in which the result is executed. The context information includes the controller, HTTP content, request context, and route data. public override void ExecuteResult(ControllerContext context) { context.HttpContext.Response.StatusCode = 303; context.HttpContext.Response.RedirectLocation = _url; }}

然后Action中使用它,来实现Http 303的重定向。:

[HttpPost]public ActionResult LoginVerify(string fusername, string fpassword){    return new SeeOtherRedirectResult(Url.Action("Index", "Security", new { fusername = fusername }));}

运行时,我们来看Http Response RAW:

HTTP/1.1 303 See Other

Cache-Control: private
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 03:05:37 GMT
Content-Length: 0

完了,希望对您Web开发有帮助。如有任何问题请留言!

您可能感兴趣的文章:

 

作者:
出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-。

转载于:https://www.cnblogs.com/wintersun/archive/2012/03/24/2415349.html

你可能感兴趣的文章
USACO 2.2 Runaround Numbers
查看>>
利用 force index优化sql语句性能
查看>>
Matlab画图-非常具体,非常全面
查看>>
365. Water and Jug Problem
查看>>
SQL数据库数据检索top和distinct
查看>>
平衡搜索树--红黑树 RBTree
查看>>
sqlite驱动下载
查看>>
让IE6/IE7/IE8浏览器支持CSS3属性
查看>>
队列实现霍夫曼树
查看>>
【Java】图片高质量缩放类
查看>>
Python :类中设置默认属性并修改
查看>>
磁盘管理综合测试
查看>>
Unity3d Shader开发(三)Pass(Pass Tags,Name,BindChannels )
查看>>
UMLet
查看>>
从父控件移除控件
查看>>
calc()制作自适应布局
查看>>
Markdown-写作必备
查看>>
关于在Java中 a!=a 值为真的解释(摘抄)
查看>>
C#串口小助手
查看>>
详解定位与定位应用
查看>>