URL重写以及生成静态页

1.注册HTTPMODEL:

<httpModules> <add name="IHttpModer" type="IHttpJingTai" /> </httpModules>

注册 <system.webServer>节里的Handler

<add name="ReWriter" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:/Windows/Microsoft.NET/Framework/v2.0.50727/aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv2.0,bitness32" />

URL伪静态配置节

<RewriterConfig> <Rules> <!-- 这里是定义重写的角色,可以重写多个--> <RewriterRule> <LookFor>~/default.html</LookFor> <SendTo>~/default.aspx</SendTo> </RewriterRule> <RewriterRule> <LookFor>~/books_(/d+).html</LookFor> <SendTo>~/books.aspx?id=$1</SendTo> </RewriterRule> <RewriterRule> <LookFor>~/list_(/d+)_(/d+).html</LookFor> <SendTo>~/list.aspx?id=$1&category=$2</SendTo> </RewriterRule> </Rules> </RewriterConfig>

IHttpModer接口代码:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web; using System.Text; using System.Text.RegularExpressions; /// <summary> ///JingTaiModel 的摘要说明 /// </summary> public class IHttpModer :IHttpModule { public void Init(HttpApplication application) { application.BeginRequest+=new EventHandler(application_BeginRequest); } void application_BeginRequest(object sender, EventArgs e) { application_PreBeginRequest(); application_BeginRequest(); } protected virtual void application_PreBeginRequest() { return; } protected virtual void application_BeginRequest() { return; } public void Dispose() { } }

静态生成帮助类:

using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> ///IHttpJingTai 的摘要说明 /// </summary> public class IHttpJingTai : IHttpModer { protected override void application_BeginRequest() { HttpContext Context = HttpContext.Current; string url = HttpContext.Current.Request.Url.OriginalString.ToString().Substring(HttpContext.Current.Request.Url.ToString().LastIndexOf("/")); if (url.Contains("list_") && url.Contains(".html")) { string urlPath = url.Substring(6).Replace(".html", ""); //Context.Response.Write("<!--" + urlPath + "-->"); int id = 0; int.TryParse(urlPath, out id); string _id = id.ToString(); while (_id.Length <3) { _id = "0" + _id; } string _floder = "/JingtaiyeShengcheng/JingTai/"; string filename = _id + ".html"; if (System.IO.File.Exists(Context.Server.MapPath(_floder + filename))) { Context.RewritePath(_floder + filename); } else { bool b = false; string _error = ""; string pageContent = ""; try { string _url = "http://" + Context.Request.Url.Host + ":7952/JingtaiyeShengcheng/list.aspx?id=" + id.ToString(); // //return; pageContent = HttpShengcheng.WebPageContentGet(_url); //需包含title及结束才为显示成功的数据 if (pageContent.Contains("</html>")) { string _floderPath = Context.Server.MapPath(_floder); if (System.IO.Directory.Exists(_floderPath) == false) { System.IO.Directory.CreateDirectory(_floderPath); } System.IO.StreamWriter sw = new System.IO.StreamWriter(_floderPath + filename, false, System.Text.Encoding.Default); //文件不存在时,会自动创建此文件. sw.WriteLine(pageContent); sw.Close(); sw.Dispose(); sw = null; b = true; } } catch (System.Exception ex) { _error = ex.ToString(); //ICore.IHttp.ResponseEnd(_error); } if (b) { Context.RewritePath(_floder + filename, false); } else { Context.Response.Redirect("Default.aspx"); } } } } }

3.静态页面类容提取类

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.Configuration; using Context = System.Web.HttpContext; using System.Net; using System.IO; using System.Text.RegularExpressions; using System.Web.UI; /// <summary> ///HttpShengcheng 的摘要说明 /// </summary> public class HttpShengcheng { public HttpShengcheng() { // //TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 抓取网页内容 /// </summary> /// <param name="url">网页地址</param> /// <returns></returns> public static string WebPageContentGet(string url) { return WebPageContentGet(url, "gb2312"); } /// <summary> /// 抓取网页内容 /// </summary> /// <param name="url">网页地址</param> /// <param name="charset">网页编码</param> /// <returns></returns> public static string WebPageContentGet(string url, string charset) { System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); Encoding coding; if (charset == "gb2312") { coding = System.Text.Encoding.GetEncoding("gb2312"); } else { coding = System.Text.Encoding.UTF8; } System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), coding); string s = reader.ReadToEnd(); reader.Close(); reader.Dispose(); response.Close(); reader = null; response = null; request = null; return s; } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值