c# URL重写基本原理

输入Default.aspx地址自动访问1.aspx页面信息

 

web.config文件:

<httpModules> <add name="MyHttpModule" type="MyHttpModule"/> </httpModules>

 

MyHttpModule类:

using System; using System.Web; public class MyHttpModule : IHttpModule { public void Init(HttpApplication application) { application.BeginRequest += Application_BeginRequest; } private void Application_BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; string oldPath = context.Request.Path; //请求路径为Default.aspx,自动重写定位到1.aspx if (oldPath == "/Test/Default.aspx") { context.RewritePath("/Test/1.aspx", "", "a=1"); } } public void Dispose() { } }

 

1.aspx

protected void Page_Load(object sender, EventArgs e) { //输出:/Test/1.aspx Response.Write("当前请求虚拟路径:" + Request.Path + "<br/>"); //输出:/Test/Default.aspx Response.Write("当前请求原始URL:" + Request.RawUrl); //输出:1 Response.Write("输出:" + Request.QueryString["a"] + "<br/>"); }

 

你可能感兴趣的:(String,object,C#,application,url,Class)