您好,欢迎来到96游戏网。
搜索
您的当前位置:首页asp.net下通过泛解析和伪静态实现二级域名的实现方法

asp.net下通过泛解析和伪静态实现二级域名的实现方法

来源:96游戏网


虽然最后是实现了,但身为程序员的我,却总是感觉利用其他不开源的组件自己总把握不了技术的核心。其实在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>

Copyright © 2019- 9603.com.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务