一般处理页面就是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>
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。