成都創(chuàng)新互聯(lián)公司長期為上千客戶提供的網(wǎng)站建設(shè)服務,團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為高陵企業(yè)提供專業(yè)的網(wǎng)站設(shè)計制作、網(wǎng)站制作,高陵網(wǎng)站改版等技術(shù)服務。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
一般處理頁面就是HttpHandler區(qū)域
-------------------------------封裝類庫
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Text.RegularExpressions; /* ************************** * 案例功能: * 1,URL地址欄阻止(參數(shù)為aspx則跳轉(zhuǎn)到錯誤頁面) * 2,F(xiàn)orm表達阻止(表單的值為aspx則彈出錯誤) * 3,阻止使用session ************************** */ namespace HttpModuleDome { public class MyHttpModule : IHttpModule { #region IHttpModule 成員 public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); context.AcquireRequestState += new EventHandler(context_AcquireRequestState); } //開始請求階段 void context_BeginRequest(object sender, EventArgs e) { HttpApplication application = sender as HttpApplication; HttpContext context = application.Context; //Url地址欄阻止 if (context.Request.QueryString.Count > 0) { for (int i = 0; i < context.Request.QueryString.Count; i++) { if (context.Request.QueryString[context.Request.QueryString.Keys[i]] == "aspx") { context.Response.Redirect("http://www.baidu.com"); context.Response.End(); } } } //Form表單阻止 if (context.Request.Form.Count > 0) { for (int i = 0; i < context.Request.Form.Count; i++) { if (context.Request.Form[context.Request.Form.Keys[i]] == "aspx") { context.Response.Write("<script>alert('錯誤');location.href='" + context.Request.RawUrl + "'</script>"); context.Response.End(); } } } } //進入了HttpHandler區(qū)域,已經(jīng)有了session void context_AcquireRequestState(object sender, EventArgs e) { HttpApplication application = sender as HttpApplication;//Global.asax的基類 HttpContext context = application.Context;//封裝了ASP.NET要處理的單次請求的所有信息 if (context.Session.Count > 0) { //context.Response.End();//直接跳過AcquireRequestState之后的請求,結(jié)束請求 } } #endregion } }
------------------------------------web.config里面引用
<system.web> <httpModules> <add name="MyhttpModule" type="HttpModuleDome.MyHttpModule,HttpModuleDome"/> </httpModules> </system.web>
------------------------------------也可以在Global.asax文件里面寫
<%@ Application Language="C#" %> <script runat="server"> /* *格式:以Application_開頭 */ //開始請求階段 void Application_BeginRequest(object sender, EventArgs e) { // 在應用程序啟動時運行的代碼 } void Application_Start(object sender, EventArgs e) { // 在應用程序啟動時運行的代碼 } void Application_End(object sender, EventArgs e) { // 在應用程序關(guān)閉時運行的代碼 } void Application_Error(object sender, EventArgs e) { // 在出現(xiàn)未處理的錯誤時運行的代碼 } void Session_Start(object sender, EventArgs e) { // 在新會話啟動時運行的代碼 } void Session_End(object sender, EventArgs e) { // 在會話結(jié)束時運行的代碼。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為 // InProc 時,才會引發(fā) Session_End 事件。如果會話模式設(shè)置為 StateServer // 或 SQLServer,則不會引發(fā)該事件。 } </script>
網(wǎng)站名稱:基于HttpModule擴展
URL分享:http://www.chinadenli.net/article2/jcosoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、微信公眾號、品牌網(wǎng)站建設(shè)、標簽優(yōu)化、企業(yè)建站、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)