web.config中,在 <httpModules>配置节中加入 <add name="MyHttpModule" type="Redirct,App_Code"/>其中type中的Redirct,App_Code是说在App_Code文件夹中的Redirct类中处理IHttpModule,name名字可以随便起.
然后在Redirct类中这样写
1 public class Redirct:IHttpModule
2 {
3 public Redirct()
4 {
5 //
6 // TODO: 在此处添加构造函数逻辑
7 //
8 }
9
10
11 private HttpContext _context = null;
12 public void Dispose()
13 {
14 throw new Exception("The method or operation is not implemented.");
15 }
16
17 public void Init(HttpApplication context)
18 {
19 _context = context.Context;
20 context.BeginRequest += new EventHandler(context_BeginRequest);
21 }
22
23 void context_BeginRequest(object sender, EventArgs e)
24 {
25 string requesturl = "~/admin/index.aspx";
26 string path = _context.Request.RawUrl;
27 string[] strlist = path.Split(Convert.ToChar("/")); //拆分字符串
28 //如果请求页面为Default.aspx
29 if (strlist[strlist.Length - 1] == "Default.aspx")
30 {
31 _context.RewritePath(requesturl, true);//跳转到requesturl 所指的页面
32 }
33 }
34 }
35
2 {
3 public Redirct()
4 {
5 //
6 // TODO: 在此处添加构造函数逻辑
7 //
8 }
9
10
11 private HttpContext _context = null;
12 public void Dispose()
13 {
14 throw new Exception("The method or operation is not implemented.");
15 }
16
17 public void Init(HttpApplication context)
18 {
19 _context = context.Context;
20 context.BeginRequest += new EventHandler(context_BeginRequest);
21 }
22
23 void context_BeginRequest(object sender, EventArgs e)
24 {
25 string requesturl = "~/admin/index.aspx";
26 string path = _context.Request.RawUrl;
27 string[] strlist = path.Split(Convert.ToChar("/")); //拆分字符串
28 //如果请求页面为Default.aspx
29 if (strlist[strlist.Length - 1] == "Default.aspx")
30 {
31 _context.RewritePath(requesturl, true);//跳转到requesturl 所指的页面
32 }
33 }
34 }
35