標籤:httpmodule httphandler
650) this.width=650;" title="搜狗20141112105809.png" src="http://s3.51cto.com/wyfs02/M02/50/E2/wKioL1RizPPz4KDjAADUpbZzjnc410.jpg" alt="wKioL1RizPPz4KDjAADUpbZzjnc410.jpg" />
一般處理頁面就是HttpHandler地區
-------------------------------封裝類庫
using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Text.RegularExpressions;/* ************************** * 案例功能: * 1,URL地址欄阻止(參數為aspx則跳轉到錯誤頁面) * 2,Form表達阻止(表單的值為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地區,已經有了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之後的請求,結束請求 } } #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) { // 在應用程式關閉時啟動並執行代碼 } void Application_Error(object sender, EventArgs e) { // 在出現未處理的錯誤時啟動並執行代碼 } void Session_Start(object sender, EventArgs e) { // 在新會話啟動時啟動並執行代碼 } void Session_End(object sender, EventArgs e) { // 在會話結束時啟動並執行代碼。 // 注意: 只有在 Web.config 檔案中的 sessionstate 模式設定為 // InProc 時,才會引發 Session_End 事件。如果會話模式設定為 StateServer // 或 SQLServer,則不會引發該事件。 } </script>
650) this.width=650;" style="float:none;" title="搜狗20141112110353.png" src="http://s3.51cto.com/wyfs02/M00/50/E4/wKiom1RizbPClYvwAAKBDTGews8172.jpg" alt="wKiom1RizbPClYvwAAKBDTGews8172.jpg" />
650) this.width=650;" style="float:none;" title="搜狗20141112110418.png" src="http://s3.51cto.com/wyfs02/M01/50/E2/wKioL1Rizh-AfkyEAAHP6wsZmXY669.jpg" alt="wKioL1Rizh-AfkyEAAHP6wsZmXY669.jpg" />
本文出自 “程式猿的家--Hunter” 部落格,請務必保留此出處http://962410314.blog.51cto.com/7563109/1575612
基於HttpModule擴充