asp.net中利用UrlRewrite动态生成htm页面
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-05-20
duleRewriter using System; using System.Text.RegularExpressions; using System.Configuration; using URLRewriter.Config; using System.Data; using System.Web; using System.Web.UI; 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) + "contentquot;; // 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="pathTransla |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于asp.net中利用UrlRewrite动态生成htm页面的所有评论