--- Post/redirect/get Pattern

Source: Internet
Author: User

Today, I got to know post/redirect/get pattern again. Thanks to the Session of hip hop, I have clarified my concepts and ideas.

 

When talking about pattern, we must first know what it is for: PRG (see link 1) is a solution provided to solve the double submit problem on the web page.

 

1. Double submit Problem

 

Context: The user enters the pagea page in the browser and clicks the submit button to go to The pageb page, prompting "submitted successfully". If the following three cases are displayed:

1> reload or refresh pageb;

2> click the back button of the browser and then click the forward button;

3> click the back button of the browser to return to pagea and click the submit button again;

What happens to websites directly developed without considering this problem?

 

Case 1 and Case 2 will pop up a small window in the browser, prompting "the page may be submitted repeatedly. Do you want to continue? "If yes is selected, the previous submission will be performed again, and Case 3 is your initiative to submit again. that is to say, if you are buying something, you have already bought the second one! This is the so-called double submit.

 

The essence is that the submit pagea and click submit button share the same server request, and this is exactly a POST request, behind the repeated submission of the page, the post request is also sent multiple times, causing the server to incorrectly add or modify data multiple times. figure 1 explains the double submit problem.

Figure 1 double submit Problem

2. PRG solusion

 

It should be thought that the user's behaviors such as "moving back and moving forward" or refreshing should be regarded as viewing historical results or pages without submitting them again.

 

One way to solve the problem is to send a POST request by clicking the submit button. The three actions of Page Submission jump and result display are separated. the PRG follows this idea. The client uses the POST method to request the server to respond to data changes. The server uses the Redirect method to specify the response to the result page of another URL, the client notifies the server of all requests displayed on the page using the get method. solution 2. in this way, the "move back and forward" or the page is refreshed with a GET request, which will not affect the server any data changes. Double submit problem can be solved.

Figure 2 PRG solusion

 

Basic Principles:

* Do not use a page directly as the response result of the POST request. You will leave a resubmit problem for those who are used to "moving back.

* After the POST request is processed, redirect the request to another URL page. The person who refreshes the page can only see it.

* Use get instead of post to request the display of a page. When you need only the display results.

 

Note:

* If you move back to the page and click Submit to submit the page, it is still considered as a voluntary submission by the user.

* After the first submission, the server's response completes the previous submission normally. If it is not blocked by the Applied framework, the server considers it another normal submission.

 

 

3. ispostback

 

It is worth mentioning that ,. in net webform, page is often used in the page_load method. the ispostback attribute is used to determine whether the current form request is the first or not, which is inconsistent with the question discussed in this article: Asp.net provides a control that supports server-side events, it also supports the autopostback action of the control, so multiple requests are sent to the current form. ispostback provides a logical branch for page loading. for details about the ispostback attribute value rules in multiple cases, refer to link 2.

 

Reference link:

[1] http://en.wikipedia.org/wiki/Post/Redirect/Get

[2] http://www.cnblogs.com/hobe/archive/2008/04/06/1139031.html

 

Http://blog.csdn.net/shaobo_wu/article/details/5854448

--- Post/redirect/get Pattern

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.