function newGuid() { var g = ""; for (var i = 0; i < 32; i++) { g += Math.floor(Math.random() * 0xF).toString(0xF); } return g; } //gets a new guid and assigns its value to the hidden field function createPageIdentifier() { var guid = this.newGuid(); document.getElementById(‘__REFRESH_FIELD‘).value = guid; }
private static Guid GetPageGuid(Page page) { string str = page.Request.Form["__REFRESH_FIELD"]; return (!string.IsNullOrEmpty(str) ? new Guid(str) : Guid.Empty); } public void Init(HttpApplication application) { guids = new Queue(queueSize); application.PreRequestHandlerExecute += new EventHandler(RefreshModule.Application_PreRequestHandlerExecute); } private static void Page_Init(object sender, EventArgs e) { Page page = sender as Page; if (page != null) { page.ClientScript.RegisterOnSubmitStatement(typeof(RefreshModule), "onsubmit", "createPageIdentifier();"); page.ClientScript.RegisterHiddenField("__REFRESH_FIELD", ""); HttpContext.Current.Items["IsRefreshed"] = false; if (page.Request.HttpMethod == "POST") { Guid pageGuid = GetPageGuid(page); bool flag = guids.Contains(pageGuid); HttpContext.Current.Items["IsRefreshed"] = flag; if (!flag && (pageGuid != Guid.Empty)) { guids.Enqueue(pageGuid); if (guids.Count > queueSize) { guids.Dequeue(); } } } } }
<httpmodules> <add name="RefreshModule" type="Jianyun.RefreshModule.RefreshModule, RefreshModule"> </add></httpmodules>
使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)
原文:https://www.cnblogs.com/dongh/p/9660909.html