Use Post/Redirect/Get to implement Asp.net to prevent repeated form submission

Source: Internet
Author: User

The previous Post mentioned the solution to form repeated submission in the Web. In fact, the problem of form repeated submission is not only Asp.net, but also other dynamic pages. Let's see the figure below:


Then, when you refresh the page, you will often see the prompt box in IE:

Google Chrome:

Firefox:

 
The simplest solution is to use the Post-Redirect-Get mode, that is, after the Http-Post is complete, Redirect is performed immediately, and the next page is Get. At this time, it is useless for the user to force press F5 to refresh. The final implementation:

 
So what should I do in Asp.net MVC? Let's look at the simple View code below:


A form that contains two inputs:
<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 is used to perform Get operations here, And LoginVerify is the target Action of Post.
[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 ();
}


The HTTP Request RAW corresponding to the Request is as follows:
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-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> <H2> Object moved to <a href = "/Security? Fusername = admin "> here </a>. </Body> Currently, Http 302 redirection is used in most Web applications. The reference of Http 1.1 in the HTTP 303 manual is used to cope with the scenario where users can refresh the form in the browser after submitting it. HTTP 303 makes the following sense:
Used to tell the client that the resource shocould be fetched using a different URL. This
New URL is in the Location header of the response message. Its main purpose is
Allow responses to POST requests to direct a client to a resource.
 
In Asp.net MVC, You can implement a custom ActionResult:
/// <Summary>
/// SeeOtherRedirectResult
/// </Summary>
Public class SeeOtherRedirectResult: ActionResult
{
Private string _ url;

/// <Summary>
/// Initializes a new instance of the <see cref = "SeeOtherRedirectResult"/> class.
/// </Summary>
/// <Param name = "url"> Target URL. </param>
Public SeeOtherRedirectResult (string url)
{
_ Url = url;
}

/// <Summary>
/// Enables processing of the result of an action method by a custom type that inherits from the <see cref = "T: System. Web. Mvc. ActionResult"/> class.
/// </Summary>
/// <Param name = "context"> The context in which the result is executed. the context information includes des the controller, HTTP content, request context, and route data. </param>
Public override void ExecuteResult (ControllerContext context)
{
Context. HttpContext. Response. StatusCode = 303;
Context. HttpContext. Response. RedirectLocation = _ url;
}
}

Then use it in the Action to implement Http 303 redirection. :
[HttpPost]
Public ActionResult LoginVerify (string fusername, string fpassword)
{
Return new SeeOtherRedirectResult (Url. Action ("Index", "Security", new {fusername = fusername }));
}

When running, let's look at Http Response RAW:
HTTP/1.1 303 See Other
Cache-Control: private
Location:/Security? Fusername = admin
Server: Microsoft-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

It will be helpful for your Web development. If you have any questions, please leave a message!
 

Author: Petter Liu
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.