In the case of slow or slow web response, most people will click to submit the form if it takes more than 5 seconds to submit. This not only results in incorrect data, but also increases the pressure on the server.
By using HttpModule, we can detect some flags before the form is processed, thus preventing users from submitting data repeatedly, and then using some interfaces to let the user handle the repeated submissions themselves, how to tell the user.
By using HttpModule, we can also use Div to overwrite the form when submitting the client form, and prevent the user from clicking the submit again from the UI layer (the user cannot F5 it directly).
This method is simple enough to place scripts and pictures directly in the specified directory, and then add a module to the web.config
<!--防止重复提交 LOG记录MODULE -->
<add name="NonReduplicatePostModule" type="tests.NonReduplicatePostModule,test"/>
Here is the implementation code:
Summary description of the ///nonreduplicatepostmodule.
///</summary>
public class NonReduplicatePostModule:System.Web.IHttpModule
{
private static ILog log = Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
Private Const string hiddenfilename = "__nonreduplicatepostmodule__";
Private Const string Maskdivscriptrelativeurl = "~/js/maskdiv.js";
Private Const string onformsubmit = "Evlon.MaskDiv.Instance.show ();";
private HttpApplication context = null;
#region IHttpModule member
public void Init (HttpApplication context)
{
This.context = context;
this.context.prerequesthandlerexecute+=new EventHandler (Context_prerequesthandlerexecute);
}
public void Dispose ()
{
this.context.prerequesthandlerexecute-=new EventHandler (Context_prerequesthandlerexecute);
}
#endregion
private void Context_prerequesthandlerexecute (object sender, EventArgs e)
{
HttpApplication webApp = sender as HttpApplication;
if (webApp!= null)
{
//has been processed, prompting the user not to repeat the submission
Page page = webApp.Context.Handler as page;
if (page!= null)
{
page. Prerender+=new EventHandler (Page_PreRender);
//Find page, add time
if (Webapp.request.form[hiddenfilename]!= null)
{
string flag = Webapp.request.form[hiddenfilename]. ToString ();
if (webApp.Context.Cache.Get (flag)!= null)
{
log. Debug ("Found Reduplicate post");
If (page is Ireduplicateposthandler)
{
WebApp.Context.Handler = new Reduplicateposthandler ((Ireduplicateposthandler) page);
}
Else
{
WebApp.Context.Handler = new Reduplicateposthandler ();
}
}
Else
{
//Put into the cache, indicating that it has been processed and automatically moved after one minute (can be submitted again)
WebApp.Context.Cache.Add (Flag,datetime.now,null,system.web.caching.cache.noabsoluteexpir Ation,timespan.fromminutes (1), system.web.caching.cacheitempriority.normal,null);
}
}
}
}
}