Click here to Skip to main content
6,630,289 members and growing! (14,825 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

.NET implementation of the SynchronizerToken pattern

By Fred Wang

An article on resolving the issue of unintentional web form resubmission.
C#, Windows, .NET 1.1, ASP.NET, WinForms, WebForms, VS.NET2003, Dev
Posted:5 Oct 2005
Updated:7 Oct 2005
Views:14,508
Bookmarked:24 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 2.27 Rating: 3.25 out of 5
2 votes, 40.0%
1

2
1 vote, 20.0%
3

4
2 votes, 40.0%
5

Sample Image

Introduction

Joe was doing online shopping, he clicked the Submit button to place an order, there was no response for 5 seconds, he thought something had gone wrong and clicked the button again!

What happened?

Joe probably got charged twice!! It was only because he could not receive the response for whatever reason, but the sever got two requests from him and placed duplicate orders for him.

Here is a solution which can prevent such kind of mistakes.

Background

Basically, this is a .NET implementation of the SynchronizerToken pattern which has been used in the Java world for years (refer to Core J2EE Patterns).

Using the code

It is pretty easy to use:

  1. add a reference to the SynchronizerToken assembly in your project.
  2. inherit you page from the Synchronizer page.
  3. add the [OneSubmitOnly] attribute right before your page class.
// Default message will be shown when duplicated

// submission happened if no RedirectURL property set. 

//[OneSubmitOnly]

// Request will be redirected to ErrorPaga.aspx

// when duplicated submission happened. 

[OneSubmitOnly(RedirectURL = "~/ErrorPage.aspx")] 
public class WebForm1 : Synchronizer 
{
  .......
}

Points of Interest

  1. The OneSumbmitOnly attribute has an optional RedirectURL property which points to any valid URL to where a request gets redirected when duplicated submission happens. If no RedirectURL property is set, the request gets rejected and the default message will show up.
  2. Resubmission is checked before any subclass event.

History

  • Version: 1.0
    • Creation date: 2005-09-30
  • Version: 1.1
    • Modified date: 2005-10-5

      Found a way to make resubmission check happen before any subclass event happens by overriding the OnLoad event handler. Now there is no worry about silly things happening in the subclass' Page_OnLoad method before the resubmission check.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Fred Wang


Member

Occupation: Web Developer
Location: Canada Canada

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralMy vote of 3 Pinmemberaphazel3:12 13 Nov '09  
GeneralThanks, this is very usefull. PinmemberJim Taylor0:18 14 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Oct 2005
Editor: Smitha Vijayan
Copyright 2005 by Fred Wang
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project