In the Web application, there is a question: "How can I tell if an HTTP request is being refreshed by pressing F5 or is it a normal commit request?" "It's not a very easy question to know who knows what I'm talking about," I believe. That is because the stateless nature of the HTTP protocol does not allow the state to persist between requests.
I think most people are concerned about this because, when the page is post or after, do not want the browser to repeat the submission. So the question can be simplified as: "How can I tell if a POST request is triggered by the F5 button or is the normal page interaction?" Fortunately, a simple detail of the DOM can be used to solve this problem. That is, when you interact with the normal page, post some data to the background, it will trigger the form of the onsubmit event, and when you press the F5 button to re-post the same page to the server side, it does not trigger the event. So far, in the mainstream browser, Ie6/7/8,firefox 3.x,chrome, and so on, is such a child.
Using the above discovery, we can solve the above problem as follows: 1. In the onsubmit event of the form (this event is called every time the page form is submitted), a GUID is generated and the GUID is assigned to a hidden Field on the page. So each time the page is submitted, we can view the value of this hidden field on the server side, if it is F5 refresh, the value in this hidden field is the value that was generated and saved when the last page was submitted.
onfunction NewGuid () { Geneva varg ="";Geneva for(vari =0; I < +; i++) {GenevaG + = Math.floor (Math.random () *0xF). ToString (0xF); to } . returnG; - } , the //gets a new GUID and assigns its value to the hidden fieldTenfunction Createpageidentifier () { One varGUID = This. NewGuid (); Adocument.getElementById ('__refresh_field'). Value =GUID; -}
2. On the server side, we maintain a queue that stores the GUID generated by each page submission. Each time the page is post back, it reads the value in the hidden field, and if the value already exists in the queue, the page is refreshed by F5. Because of the need to intercept HTTP requests, we can use a httpmodule that can register hidden field and determine some of the operations in HttpModule.
on Private StaticGuid getpageguid (Page page) Geneva {Geneva stringstr = page. request.form["__refresh_field"];Geneva return(!string. IsNullOrEmpty (str)?NewGuid (str): Guid.Empty); to } . - Public voidInit (HttpApplication application) , { theGUIDs =NewQueue (queuesize);Ten OneApplication. PreRequestHandlerExecute + =NewEventHandler (refreshmodule.application_prerequesthandlerexecute); A } - - Private Static voidPage_Init (Objectsender, EventArgs e) the { -Page page = Sender asPage; - if(Page! =NULL) - { +Page. Clientscript.registeronsubmitstatement (typeof(Refreshmodule),"onsubmit","createpageidentifier ();"); -Page. Clientscript.registerhiddenfield ("__refresh_field",""); + Ahttpcontext.current.items["isrefreshed"] =false; at if(page. Request.httpmethod = ="POST") - { -Guid Pageguid =getpageguid (page); - BOOLFlag =GUIDs. Contains (pageguid); -httpcontext.current.items["isrefreshed"] =Flag; - if(!flag && (pageguid! =guid.empty)) in { -GUIDs. Enqueue (pageguid); to if(GUIDs. Count >queuesize) + { -GUIDs. Dequeue (); the } * } $ }Panax Notoginseng } -}
HttpModule needs to be registered in Web. config.
1 2 <add name="refreshmodule" type= " Jianyun.RefreshModule.RefreshModule, Refreshmodule ">3 </add>
Complete code and demo download: refreshmodule.zip (download number: 181)original articles, reproduced please specify: reproduced from the busy Cloud blog
This article link address: use a httpmodule to intercept HTTP requests to detect page refreshes (F5 or normal requests)
Use a HttpModule to intercept HTTP requests to detect page refreshes (F5 or normal requests)