使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)
01 function newGuid() { 02 var g = ""; 03 for (var i = 0; i < 32; i++) { 04 g += Math.floor(Math.random() * 0xF).toString(0xF); 05 } 06 return g; 07 } 08 09 //gets a new guid and assigns its value to the hidden field 10 function createPageIdentifier() { 11 var guid = this.newGuid(); 12 document.getElementById(‘__REFRESH_FIELD‘).value = guid; 13 }
01 private static Guid GetPageGuid(Page page) 02 { 03 string str = page.Request.Form["__REFRESH_FIELD"]; 04 return (!string.IsNullOrEmpty(str) ? new Guid(str) : Guid.Empty); 05 } 06 07 public void Init(HttpApplication application) 08 { 09 guids = new Queue(queueSize); 10 11 application.PreRequestHandlerExecute += new EventHandler(RefreshModule.Application_PreRequestHandlerExecute); 12 } 13 14 private static void Page_Init(object sender, EventArgs e) 15 { 16 Page page = sender as Page; 17 if (page != null) 18 { 19 page.ClientScript.RegisterOnSubmitStatement(typeof(RefreshModule), "onsubmit", "createPageIdentifier();"); 20 page.ClientScript.RegisterHiddenField("__REFRESH_FIELD", ""); 21 22 HttpContext.Current.Items["IsRefreshed"] = false; 23 if (page.Request.HttpMethod == "POST") 24 { 25 Guid pageGuid = GetPageGuid(page); 26 bool flag = guids.Contains(pageGuid); 27 HttpContext.Current.Items["IsRefreshed"] = flag; 28 if (!flag && (pageGuid != Guid.Empty)) 29 { 30 guids.Enqueue(pageGuid); 31 if (guids.Count > queueSize) 32 { 33 guids.Dequeue(); 34 } 35 } 36 } 37 } 38 }
1 <httpmodules> 2 <add name="RefreshModule" type="Jianyun.RefreshModule.RefreshModule, RefreshModule"> 3 </add></httpmodules>
原创文章,转载请注明: 转载自闲云博客
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。