- public void ProcessRequest(HttpContext context)
- {
- string rawUrl = context.Request.RawUrl;
- string requestPath = context.Request.Path;
- string applicationPath = context.Request.ApplicationPath;
- Url urlItem = null;
- //上下文中没有定义ToStaticUrlItem表示,此请求没有经过UrlRewrite,直接编译,不生成html
- //参考UrlRewriteModule.cs
- if (context.Items["ToStaticUrlItem"] == null)
- {
- if (!File.Exists(context.Request.PhysicalPath))
- {
- throw new HttpException(404, "您访问的页面没有找到。");
- }
- // asp.net 1.1 采用下面方法编译页面
- //PageParser.GetCompiledPageInstance(requestPath, context.Request.PhysicalPath, context).ProcessRequest(context);
- IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(requestPath, typeof(Page)) as IHttpHandler;
- hander.ProcessRequest(context);
- return;
- }
- string filePath;
- urlItem = (Url)context.Items["ToStaticUrlItem"];
- Regex regex = new Regex(
- Globals.ApplicationPath + urlItem.LookFor,
- RegexOptions.CultureInvariant | RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
- string requestFile = regex.Replace(rawUrl, Globals.ApplicationPath + urlItem.WriteTo.Replace("^", "&"));
- if (requestFile.IndexOf("?") > 0)
- {
- filePath = requestFile.Substring(0, requestFile.IndexOf("?"));
- }
- else
- {
- filePath = requestFile;
- }
- string inputFile = context.Request.PhysicalApplicationPath + filePath;
- string path = context.Request.PhysicalApplicationPath + rawUrl.ToLower().Replace(".aspx", ".html");
- if (applicationPath != "/")
- {
- inputFile = inputFile.Replace(applicationPath + "/", @"/");
- path = path.Replace(applicationPath + "/", "").Replace("/", @"/");
- }
- else
- {
- path = path.Replace("/", @"/");
- }
- if (!urlItem.EnabledToStatic)
- {
- // asp.net 1.1 采用下面方法编译页面
- //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );
- IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;
- hander.ProcessRequest(context);
- return;
- }
- if (!File.Exists(path))
- {
- // asp.net 1.1 采用下面方法编译页面
- //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );
- IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;
- hander.ProcessRequest(context);
- context.Response.Filter = new AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter, path);
- return;
- }
- if (urlItem.Minutes == Int32.MaxValue)
- {
- context.Server.Transfer(rawUrl.ToLower().Replace(".aspx", ".html"));
- }
- else
- {
- FileInfo fileInfo = new FileInfo(path);
- if (fileInfo.LastWriteTime.AddMinutes((double)urlItem.Minutes) < DateTime.Now)
- {
- fileInfo.Delete();
- // asp.net 1.1 采用下面方法编译页面
- //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );
- IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;
- hander.ProcessRequest(context);
- context.Response.Filter = new AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter, path);
- }
- else
- {
- context.Server.Transfer(rawUrl.ToLower().Replace(".aspx", ".html"));
- }
- return;
- }
- }
源码下载:ASP.NET生成静态页面