国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 2.0 中的URL重寫以及urlMappings問題

2024-07-10 12:40:10
字體:
來源:轉載
供稿:網友
在asp.net2.0中的urlMappings倒是非常好用,可惜暫不支持正則表達式,不過,好在如果用IHttpModule的話 

不管什么樣的請求都會先經過IHttpModule這樣就為URL重寫提供了一個好機會: 

下面是我寫的一個IHttpModule: 

using System; 
using System.Web; 

public class ReWriteModule:IHttpModule 

public ReWriteModule() 


public override string ToString() 

return this.GetType().ToString(); 



void IHttpModule.Dispose() 



private static System.Xml.XmlDocument ruleDoc = null; 
private static System.Xml.XmlDocument GetRuleConfig(System.Web.HttpContext app) 

if (ruleDoc == null) 

ruleDoc = new System.Xml.XmlDocument(); 
ruleDoc.Load(app.Server.MapPath("~/rule.xml")); 

return ruleDoc; 

public static string GetUrl(System.Web.HttpContext cxt,string path) 


System.Xml.XmlDocument doc = GetRuleConfig(cxt); 
System.Xml.XmlNodeList lst= doc.GetElementsByTagName("RewriterRule"); 
string pat=""; 
foreach (System.Xml.XmlNode nd in lst) 

System.Xml.XmlNodeList sub = nd.ChildNodes[0].ChildNodes; 
foreach(System.Xml.XmlNode chk in sub) 

pat = "^" + chk.InnerText+"$"; 
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pat, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
if(reg.IsMatch(path)) 

return reg.Replace(path, nd.ChildNodes[1].InnerText); 



return null; 


void IHttpModule.Init(HttpApplication context) 

context.BeginRequest += delegate(object sender, EventArgs e) 



System.Web.HttpContext cxt = context.Context; 

if (cxt.Request.ContentType != "image/pjpeg") 

string type = cxt.Request.ContentType.ToLower(); 
string path = cxt.Request.Path; 
string apppath = cxt.Request.ApplicationPath; 
path = path.Remove(0, apppath.Length); 
path = "~" + path; 

string newUrl = GetUrl(cxt, path.TrimEnd().TrimStart()); 
if (newUrl != null) 

cxt.Response.Filter = new ResponseFilter(cxt.Response.Filter,cxt.Request.Path); 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜新市| 南华县| 海淀区| 庄河市| 获嘉县| 西乌珠穆沁旗| 渝北区| 玉溪市| 大洼县| 怀宁县| 江源县| 凤山市| 凤阳县| 城口县| 海城市| 滨海县| 大方县| 云浮市| 吐鲁番市| 贡觉县| 高邮市| 德保县| 木里| 阿城市| 社会| 五大连池市| 仙居县| 化德县| 汉沽区| 应用必备| 威海市| 西峡县| 乳山市| 汉川市| 东光县| 临沭县| 崇仁县| 拉孜县| 兰考县| 黑龙江省| 鲁山县|