void Application_BeginRequest(object sender, EventArgs e)
{
//1.0 初始正则表达式的对象,它只能匹配以 /index开头 带两个参数
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("/index/(.*)/(.*)");
HttpContext.Current.Response.Write("Application_Start");
//2.0 获取浏览器请求上来原始url的路径 /index/1/abc
string urlpath = HttpContext.Current.Request.RawUrl; // /index/1/abc
HttpContext.Current.Response.Write(urlpath);
HttpContext.Current.Response.End();
// 3.0 利用正则表达式来匹配当前的url 如果成功匹配,则将url重写asp.net能够正常解析url
if (reg.IsMatch(urlpath))
{
urlpath = reg.Replace(urlpath, "/Default.aspx?id=$1&name=$2"); //index.aspx?id=1&name=abc
// 4.0 将重写后的urlpath写回
HttpContext.Current.RewritePath(urlpath);
}
}
{
//1.0 初始正则表达式的对象,它只能匹配以 /index开头 带两个参数
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("/index/(.*)/(.*)");
HttpContext.Current.Response.Write("Application_Start");
//2.0 获取浏览器请求上来原始url的路径 /index/1/abc
string urlpath = HttpContext.Current.Request.RawUrl; // /index/1/abc
HttpContext.Current.Response.Write(urlpath);
HttpContext.Current.Response.End();
// 3.0 利用正则表达式来匹配当前的url 如果成功匹配,则将url重写asp.net能够正常解析url
if (reg.IsMatch(urlpath))
{
urlpath = reg.Replace(urlpath, "/Default.aspx?id=$1&name=$2"); //index.aspx?id=1&name=abc
// 4.0 将重写后的urlpath写回
HttpContext.Current.RewritePath(urlpath);
}
}