我实现IHttpModule进行URL重写 public class UrlReWriter:IHttpModule
{
public UrlReWriter(){}
public void Dispose()
{
throw new NotImplementedException();
} public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.Error += new EventHandler(context_Error);
}
void context_Error(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
context.Response.Write("<html>");
context.Response.Write("<head><title>出错了!</title></head>");
context.Response.Write("<body style=\"font-size:14px;\">");
context.Response.Write("出错了:<br />");
context.Response.Write("<textarea name=\"errormessage\" style=\"width:80%; height:200px; word-break:break-all\">");
context.Response.Write(HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
context.Response.Write("</textarea>");
context.Response.Write("</body></html>");
context.Response.End();
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpResponse response = context.Response;
string path = context.Request.Path;
string file = System.IO.Path.GetFileName(path); string oldfile = Regex.Replace(file, @"\d+", ""); Regex[] regexs = new Regex[]
{
new Regex("djyx(\\d+).aspx", RegexOptions.Compiled),
new Regex("wlyx(\\d+).aspx", RegexOptions.Compiled),
new Regex("glmj(\\d+).aspx", RegexOptions.Compiled),
new Regex("yxzx(\\d+).aspx", RegexOptions.Compiled),
new Regex("yxpc(\\d+).aspx", RegexOptions.Compiled),
new Regex("CreativeFun(\\d+).aspx", RegexOptions.Compiled),
new Regex("News(\\d+).aspx", RegexOptions.Compiled)
}; foreach (Regex rex in regexs)
{
Match match = rex.Match(file); if (match.Success)
{
string userId = match.Groups[1].Value;
string rewritePath = oldfile+ "?index=" + userId;
context.RewritePath(rewritePath); break;
}
}
}}
效果是实现了 但是我在HttpApplication application = (HttpApplication)sender;打了个断点 居然跟循环一样 执行完方法了 还定位到了HttpApplication application = (HttpApplication)sender;这个地方继续重复执行 大概又重复了20来次才算好 为什么为什么 。这样影响效率
{
public UrlReWriter(){}
public void Dispose()
{
throw new NotImplementedException();
} public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.Error += new EventHandler(context_Error);
}
void context_Error(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
context.Response.Write("<html>");
context.Response.Write("<head><title>出错了!</title></head>");
context.Response.Write("<body style=\"font-size:14px;\">");
context.Response.Write("出错了:<br />");
context.Response.Write("<textarea name=\"errormessage\" style=\"width:80%; height:200px; word-break:break-all\">");
context.Response.Write(HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
context.Response.Write("</textarea>");
context.Response.Write("</body></html>");
context.Response.End();
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpResponse response = context.Response;
string path = context.Request.Path;
string file = System.IO.Path.GetFileName(path); string oldfile = Regex.Replace(file, @"\d+", ""); Regex[] regexs = new Regex[]
{
new Regex("djyx(\\d+).aspx", RegexOptions.Compiled),
new Regex("wlyx(\\d+).aspx", RegexOptions.Compiled),
new Regex("glmj(\\d+).aspx", RegexOptions.Compiled),
new Regex("yxzx(\\d+).aspx", RegexOptions.Compiled),
new Regex("yxpc(\\d+).aspx", RegexOptions.Compiled),
new Regex("CreativeFun(\\d+).aspx", RegexOptions.Compiled),
new Regex("News(\\d+).aspx", RegexOptions.Compiled)
}; foreach (Regex rex in regexs)
{
Match match = rex.Match(file); if (match.Success)
{
string userId = match.Groups[1].Value;
string rewritePath = oldfile+ "?index=" + userId;
context.RewritePath(rewritePath); break;
}
}
}}
效果是实现了 但是我在HttpApplication application = (HttpApplication)sender;打了个断点 居然跟循环一样 执行完方法了 还定位到了HttpApplication application = (HttpApplication)sender;这个地方继续重复执行 大概又重复了20来次才算好 为什么为什么 。这样影响效率
HttpContext.Current.RewritePath(rewritePath);
这样看看