您的位置:首页 > 编程语言 > ASP

用global.asax在ASP.NET实现无组件无扩展名伪静态

2011-07-07 01:50 274 查看
 

用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;
    }
   }
  }
   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息