用global.asax在ASP.NET实现无组件无扩展名伪静态
最讨厌安装组件什么了的,于是查了很多资料,总结了一个ASP.NET不用组件实现伪静态的方法。
一、IIS设置
1、在主目录-配置里,将.aspx扩展名的"确认文件是否存在"的钩去掉。
2、接着在下面的映射里,添加.aspx扩展名里的可执行文件路径。
3、默认文档一定要以.aspx为扩展名的文件名为第一个。
二、程序部分
1、在网站根目录新建global.asax。
2、添加如下代码:将saoluche指向扫路车
void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
string requestPath = context.Request.Path.ToLower();
if( requestPath.IndexOf(".") < 0 )
{
//特殊的SEO重写
Hashtable ht = new Hashtable();
//分类信息
ht.Add("/saoluche","/album.aspx?id=58");
foreach(DictionaryEntry de in ht)
{
if( requestPath.IndexOf(de.Key.ToString()) >= 0 )
{
requestPath = requestPath.Replace(de.Key.ToString(),de.Value.ToString());
if( requestPath == "/" || requestPath == "" )
requestPath = "index.aspx";
requestPath = requestPath.TrimEnd("/");
Context.RewritePath(requestPath);
break;
}
}
}
}