利用UrlRewrite,asp.net动态生成htm页面
点击次数:20 次 发布日期:2008-11-26 22:30:32 作者:源代码网
|
源代码网推荐 功夫不负有心人,终于被我找到了,只需要在web.config中进行简单配置,就可以达到动态生成静态页面的效果,同时又不影响Url重定向。web.config中需要注意的配置节为<configuration>、<RewriteConfig>、<httpModules>、<httpHandlers>,在这些配置节里边都有注释,容易看懂。 源代码网推荐 <?xml version="1.0" encoding="utf-8"?> 源代码网推荐 <!-- 源代码网推荐 注意: 除了手动编辑此文件以外,您还可以使用 源代码网推荐 Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的 源代码网推荐 “网站”->“Asp.Net 配置”选项。 源代码网推荐 设置和注释的完整列表在 源代码网推荐 machine.config.comments 中,该文件通常位于 源代码网推荐 WindowsMicrosoft.NetFrameworkv2.xConfig 中 源代码网推荐 --> 源代码网推荐 <configuration> 源代码网推荐 源代码网推荐 <!-- RUL重写开始 --> 源代码网推荐 <configSections> 源代码网推荐 <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/> 源代码网推荐 </configSections> 源代码网推荐 <RewriterConfig> 源代码网推荐 <Rules> 源代码网推荐 <!--地址重写规则--> 源代码网推荐 <!--首页,定位到静态页面--> 源代码网推荐 <RewriterRule> 源代码网推荐 <Type>Static</Type> 源代码网推荐 <LookFor>~/Default.aspx</LookFor> 源代码网推荐 <SendTo>~/Default.htm</SendTo> 源代码网推荐 </RewriterRule> 源代码网推荐 <!--二级页面,定位到动态页面--> 源代码网推荐 <RewriterRule> 源代码网推荐 <Type>Dynamic</Type> 源代码网推荐 <LookFor>~/List.aspx</LookFor> 源代码网推荐 <SendTo>~/Show.aspx</SendTo> 源代码网推荐 </RewriterRule> 源代码网推荐 </Rules> 源代码网推荐 </RewriterConfig> 源代码网推荐 <!-- RUL重写结束 --> 源代码网推荐 源代码网推荐 <appSettings/> 源代码网推荐 <connectionStrings/> 源代码网推荐 <system.web> 源代码网推荐 <!-- 源代码网推荐 设置 compilation debug="true" 将调试符号插入 源代码网推荐 已编译的页面中。但由于这会 源代码网推荐 影响性能,因此只在开发过程中将此值 源代码网推荐 设置为 true。 源代码网推荐 --> 源代码网推荐 <httpModules> 源代码网推荐 <!--URL重写--> 源代码网推荐 <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" /> 源代码网推荐 </httpModules> 源代码网推荐 源代码网推荐 <httpHandlers> 源代码网推荐 <!--生成静态页面--> 源代码网推荐 <add verb="*" path="*.aspx" validate="false" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/> 源代码网推荐 </httpHandlers> 源代码网推荐 源代码网推荐 <compilation debug="false" /> 源代码网推荐 <!-- 源代码网推荐 通过 <authentication> 节可以配置 ASP.NET 使用的 源代码网推荐 安全身份验证模式, 源代码网推荐 以标识传入的用户。 源代码网推荐 --> 源代码网推荐 <authentication mode="Forms" /> 源代码网推荐 <!-- 源代码网推荐 如果在执行请求的过程中出现未处理的错误, 源代码网推荐 则通过 <customErrors> 节可以配置相应的处理步骤。具体说来, 源代码网推荐 开发人员通过该节可以配置 源代码网推荐 要显示的 html 错误页 源代码网推荐 以代替错误堆栈跟踪。 源代码网推荐 源代码网推荐 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> 源代码网推荐 <error statusCode="403" redirect="NoAccess.htm" /> 源代码网推荐 <error statusCode="404" redirect="FileNotFound.htm" /> 源代码网推荐 </customErrors> 源代码网推荐 --> 源代码网推荐 <globalization requestEncoding="utf-8" responseEncoding="utf-8"/> 源代码网推荐 </system.web> 源代码网推荐 </configuration> 源代码网推荐 两个关键的类是ModuleRewriter和RewriterFactoryHandler 源代码网推荐 ModuleRewriter类用于Url重定向,代码如下: 源代码网推荐 ModuleRewriter 源代码网推荐 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) + "$"; 源代码网推荐 源代码网推荐 // 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 重写。 源代码网推荐 支持开源,开源万岁! 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
