Use HttpModule to disable Web Form repeat submissions

Source: Internet
Author: User
Tags add time

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.


///&lt;/summary&gt;


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);


                        }


                    }


                }


            }





        }

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.