首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> ASP.NET >> 正文

利用UrlRewrite,asp.net动态生成htm页面


  namespace URLRewriter
  {
   /**//// <summary>
   /// Provides a rewriting HttpModule.
   /// </summary>
   public class ModuleRewriter : BaseModuleRewriter
   {
   /**//// <summary>
   /// This method is called during the module's BeginRequest event.
   /// </summary>
   /// <param name="requestedRawUrl">The RawUrl being requested (includes path and querystring).</param>
   /// <param name="app">The HttpApplication instance.</param>
   protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
   {
   //只对文件后缀为aspx页面有效
   if (requestedPath.IndexOf(".aspx") != -1)
   {
   HttpContext httpContext = app.Context;
  
   // get the configuration rules
   RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
  
   // iterate through each rule
   for (int i = 0; i < rules.Count; i++)
   {
   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
   string lookFor = "^" + RewriterUtils.ResolveUrl(httpContext.Request.ApplicationPath, rules[i].LookFor) + "$";
  
   // Create a regex (note that IgnoreCase is set)
   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
  
   // See if a match is found
   if (re.IsMatch(requestedPath))
   {
   //aspx页面重定向到htm页面且http数据传输方式为“GET”
   if (rules[i].Type == WebType.Static && app.Context.Request.RequestType == "GET")
   {
   //静态页面路径
   string htmlWeb = RewriterUtils.ResolveUrl(httpContext.Request.ApplicationPath, rules[i].SendTo);
   //静态页面的最后修改时间
   DateTime dt = System.IO.File.GetLastWriteTime(httpContext.Server.MapPath(htmlWeb));
   //当前时间和静态页面生成时间对比,如果时间小于一个小时,重定向到静态页面
   if (DateTime.Compare(DateTime.Now.AddHours(-1), dt) <= 0)
   {
   requestedPath = htmlWeb;
   }
   }
   else
   {
   requestedPath = rules[i].SendTo;
   }
  
   // Rewrite the URL
   RewriterUtils.RewriteUrl(httpContext, requestedPath);
   break; // exit the for loop
   }
   }
   }
   }
  
   }
  }
  CreateHtmFactoryHandler类用于生成静态页面,代码如下:
  CreateHtmFactoryHandler
  using System;
  using System.IO;
  using System.Web.UI;
  using System.Web;
  using URLRewriter.Config;
  using System.Configuration;
  using System.Text.RegularExpressions;
  
  namespace URLRewriter
  {
   /**//// <summary>
   /// 依据web.config配置信息,生成静态页面
   /// </summary>
   public class CreateHtmFactoryHandler : IHttpHandlerFactory
   {
   /**//// <summary>
   ///
   /// </summary>
   /// <param name="context">HttpContext 类的实例,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session 和 Server)的引用</param>
   /// <param name="requestType">客户端使用的 HTTP 数据传输方法(GET 或 POST)</param>
   /// <param name="url">所请求资源的 RawUrl。</param>
   /// <param name="pathTranslated">所请求资源的 PhysicalApplicationPath</param>
   /// <returns>返回实现 IHttpHandler 接口的类的实例</returns>
   public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
   {
   if (requestType == "GET")
   {
   // get the configuration rules
   RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
  
   // iterate through each rule
   for (int i = 0; i < rules.Count; i++)
   {
   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
   string lookFor = "^" + RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rules[i].LookFor) + "$";
  
   // Create a regex (note that IgnoreCase is set)
   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
  
   // See if a match is found
   if (re.IsMatch(url) && rules[i].Type == WebType.Static)
   {
   //得到要生成的静态页面的物理路径
   string physicsWeb = context.Server.MapPath(RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rules[i].SendTo));
   //设定Response筛选器
   context.Response.Filter = new ResponseFilter(context.Response.Filter, physicsWeb);
   break;
   }
   }
   }
   //得到编译实例
   return PageParser.GetCompiledPageInstance(url, pathTranslated, context);
   }
  
   public virtual void ReleaseHandler(IHttpHandler handler)
   {
   }
   }
  }
  以上是两个主要的类,还有一些辅助的类,我把测试项目附上,里边有源代码和示例。
  点击这里下载:源代码和示例
  声明一下,我是在修改一个开源的项目(UrlRewrite)来实现上述功能。有关UrlRewrite介绍,请看这里,ASP.NET 中执行 URL 重写。

上一页  [1] [2] 

【责编:John】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航