How does ASP. NET prevent repeated sending of pages during page reorganization...

Source: Internet
Author: User
Some users' behaviors are really hard to guess ..., You can refresh the page if you have something to do. This action seems harmless, but when you have just executed the submit, the refresh page will be executed repeatedly, this is also why the message "do not close the Web page or reorganize to avoid transaction failures or repeated transactions" is prompted during Transaction Payment on major shopping websites. However, based on experience, even if a warning is raised on the webpage, there are still many users still refresh the webpage. -

Note: Do not think that only ASP. net. This problem is common in Web applications. No matter what platform or language you use for development, this is because browsers automatically cache users' browsing behaviors (including materials ), the same is true for IE, Firefox, and chrome tests. It is assumed that the success history of previous and next pages can be established. For further exploration, I will not go into further research if I am not able to do so. (You are welcome to give some advice from senior experts and tell me why )... Since the browser was designed like this, and we may never guess how users love to operate it, let's look at ASP. NET development to see how this problem can be solved.

I don't know if anyone is the same as me. What I think right away is: re-orientation, that is, executing response after a job is successfully executed. this is the most typical practice of redirect method re-importing to the result page, but it is applicable to multiple page forms with consistent actions, such as: shopping cart, after the checkout is completed, you can export the order success message page... When the checkout screen is reached, there is no difference in importing more data. Unfortunately, after all, there are a few multiple web forms. Most Web applications now require almost all non-synchronous updates (Ajax). It is best to complete all the actions on the same screen, even if you do not require non-synchronous updates today, after each job is completed, it is not ideal to export to another web page. Therefore, this method is not perfect, except for the trouble of maintaining another web page. In fact, if the user returns to the previous page and reorganizes it, the same may cause repeated execution ....

Check whether the same data exists before any change? In other words, it should be feasible to check the database,... The process seems to be a little tedious. If you want to write the logic for comparing the same data for each assignment, you will feel tired ..., In addition, sometimes the same information can be allowed to exist. For example, if the message board of online customer service is impatient, the user will leave a message again, and the content may be the same as the previous one, this is incomparable with repeated data on the refresh page. It seems that it is not a good practice to exclude the same data on the database...

The key point is how to identify that the user is refreshing the page. Further, is there a permanent approach that allows us to check a certain attribute to determine whether the page is being returned to avoid repeated sending actions? After thinking about it, there was no new things in the sun, and I searched for it online. There were several articles and discussion strings outside of China that proposed several solutions to this problem (the above two practices proved to be recommended by some people ), I think the most important part is the following two articles:

  • Build your ASP. NET pages on a richer Bedrock
  • Preventing duplicate record insertion on page refresh

    Refer to the content of the above two articles and get the final answer: we can inherit the page category of ASP. NET and expand the required functions on our own! The procedure is as follows:

    1. Inherit System. Web. UI. Page and customize a basepage category.

    Reference content is as follows:
    1. Using system;
    2. /// <Summary>
    3. /// Basepage summary description
    4. /// </Summary>
    5. Public class basepage: system. Web. UI. Page
    6. {
    7. Public basepage (){}
    8. }

    2. Write the setactionstamp method under the basepage category to store a system time stamp in the session.

    Reference content is as follows:
    1. /// <Summary>
    2. /// Set the timestamp
    3. /// </Summary>
    4. Private void setactionstamp ()
    5. {
    6. Session ["actionstamp"] = server. urlencode (datetime. Now. tostring ());
    7. }

    3. Handle the prerender event. Set the stamp when the webpage is loaded for the first time. The stamp is stored in hiddenfield every time it is loaded and executed.

    Reference content is as follows:
    1. Public basepage () {This. prerender + = new eventhandler (page_prerender );}
    2. Void page_prerender (Object sender, eventargs E)
    3. {
    4. If (! Ispostback)
    5. {
    6. Setactionstamp ();
    7. }
    8. Clientscript. registerhiddenfield ("actionstamp", session ["actionstamp"]. tostring ());
    9. }

    4. Customize the isrefresh attribute. By judging whether the stamp stored in the hiddenfield is equal to the value stored in the session, you can determine whether the webpage is returned by reorganizing the operation.

    Reference content is as follows:
    1. /// <Summary>
    2. /// Obtain the value, indicating whether the webpage is returned by the reorganizing action (PostBack)
    3. /// </Summary>
    4. Protected bool isrefresh
    5. {
    6. Get
    7. {
    8. If httpcontext. Current. request ["actionstamp"] as string = session ["actionstamp"] as string)
    9. {
    10. Setactionstamp ();
    11. Return false;
    12. }
    13. Return true;
    14. }
    15. }

    When you write a Web page program later, you can obtain the isrefresh attribute value as long as it is derived from the basepage. It can be used to determine whether the page is reorganized to avoid repeated operations:

    The concept is as follows: when a webpage is loaded for the first time, we record the timestamp in the session and copy the timestamp to the hiddenfield for storage. When the page is reversed, the timestamp is always set for the first time, until a certain action is sent, we want to identify whether it is sent by rearranging. Therefore, the isrefresh attribute is determined. When the first request is sent, false is returned, and the job can be executed smoothly, only the timestamps in the session are updated. At this time, the values in the hiddenfield are different. The interesting thing is that refresh sends the previous action again, but because the browser caches the status, the time stamp in the hiddenfield is still an old value, different from the one held by the session, check whether the attribute value of isrefresh is true. To avoid repeated execution, you can block the action.

    So far, this article has raised a solution to the question raised by the title and briefly explained the concept. However, the two reference articles provided above are actually described in detail, anyone who wants to know about it is recommended to take a look, especially the first article written by the master Dino Esposito.

    Finally, I will talk about some tips mentioned in a master Dino article. Since we have extended a basepage category with the function of checking page reorganization, so how can we make the new web form presets derived from the basepage? You can open <pages "under <system. Web" in the web. config file to set the pagebasetype attribute. For example:

    After the settings are complete, the newly added web form will be inherited from the basepage.

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.