虽然最后是实现了,但身为程序员的我,却总是感觉利用其他不开源的组件自己总把握不了技术的核心。其实在net中微软已经为我们留下了接口,让我们为所欲为了。
首先我们可以通过一张图大概了解下.net的生命周期。 

从 上图可以看出来,针对每个不同用户的请求,服务器都会创建一个新的HttpContext实例直到请求结束,服务器销毁这个实例。而 Ihttpcontext是httpcontext对外公开的接口,它包含了2个方法:dispose()和Init(HttpApplication context),我们可以实现Ihttpcontext从而达到httpcontext。 
关键代码: 
复制代码 代码如下: 
HttpApplication app = (HttpApplication)sender; 
HttpContext context = app.Context; 
string url = context.Request.Url.AbsoluteUri; //完整url 
string turl = url.Split('.')[0]; 
string surl = turl.ToLower().Replace("http://", ""); 
StringBuilder strb = new StringBuilder(); 
strb.Append(url); 
strb.Append(surl);  
app.Context.RewritePath(path, string.Empty, strb.ToString().Split('?')[1]); 
在web.config里配置下: 
<system.web>里添加如下代码。 
<httpModules> 
<add type="Common.URLRewriter" name="Common" /> 
最后设置IIS的时候记得要把IIS的表头设置为空。 
运行下你就能实现了 
</httpModules>