|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
有理由相信是能提供更出色的性能。很多平台无法支持复杂的编译器,因此需要二次编译来减少本地编译器的复杂度。当然可能做不到java编译器那么简易。asp.netAnewfeatureinAsp.Net2.0isitsbuilt-inurlrewritingsupport.Whenilookedintothisnewfeatureifoundoutitlackedregularexpressionssupport,wichisreallythepointofanUrlMapper.ScottGluathisblog,explainsthereasonwhytheAsp.Netteamdidntimplementedthisfeature.BasicallytheyrealizedthatafullfeaturedversionwouldwanttotakeadvantageofthenextIIS7.0newfeatures,speciallythesupportforallcontent-types(imagesanddirectories).
Anyway,itsreallysimpletoimplementaUrlRewritingModulewithRegexsupportinAsp.Net.IwroteaquickandsimpleHttpModuleforthis.ThewholemagicisdonewithinafewlineswithintheHttpModule:
1publicvoidRewrite_BeginRequest(objectsender,System.EventArgsargs){
2stringstrPath=HttpContext.Current.Request.Url.AbsolutePath;
3UrlRedirectionoPR=newUrlRedirection();
4stringstrURL=strPath;
5stringstrRewrite=oPR.GetMatchingRewrite(strPath);
6if(!String.IsNullOrEmpty(strRewrite)){
7strURL=strRewrite;
8}else{
9strURL=strPath;
10}
11HttpContext.Current.RewritePath("~"+strURL);
12}
Thecodeisselfexplanatory.WhenarequestthatisprocessedbytheAsp.Netengine,themodulechecksanxmlforaregexmatch.IveseenmanyUrlRewritingenginesthatusesWeb.configtostorethematchingrulesbutipreferusinganadditionalxmlfile.Therewritingrulesfilelooklikethefollowing:
1<?xmlversion="1.0"encoding="utf-8"standalone="yes"?>
2<urlrewrites>
3<rulename="CategoryPage">
4<url>/([a-zA-Z][w-]{1,149}).aspx</url>
5<rewrite>/Default.aspx?Category=$1</rewrite>
6</rule>
7<rulename="ItemPage">
8<url>/([a-zA-Z][w-]{1,149})/([a-zA-Z][w-]{1,149}).aspx</url>
9<rewrite>/Default.aspx?Category=$1&Item=$2</rewrite>
10</rule>
11</urlrewrites>
Therulematchingroutine,wichisimplementedintheGetMatchingRewrite()methodisquitesimpleandlightweighted:
1publicstringGetMatchingRewrite(stringURL){
2stringstrRtrn="";
3
4System.Text.RegularExpressions.RegexoReg;
5
6foreach(RedirectRuleoRuleinRules){
7
8Reg=newRegex(oRule.URL);
9MatchoMatch=oReg.Match(URL);
10
11if(oMatch.Success){
12strRtrn=oReg.Replace(URL,oRule.Rewrite);
13}
14
15}
16returnstrRtrn;
17}
Ihaveuploadedasampleprojectthatusesthisrewritingengine.TheHttpModuleanditshelperclassesareinsidetheApp_Codefolder.Ihopeyoufindthiscodeuseful,ifyouhaveanyquestionsjustleaveacommentinthisentry.Happycoding!
--------------------------------------------------------------------------------
FROMDEVEL.oping.net
postedon2006-04-2614:17徐灿钊Asp.net专栏浏览(48)批评(1)编纂保藏保藏至365Key所属分类:.net2.0
批评:
#re:UrlRewritingwithRegexforASP.NET2.0(在asp.net2.0中利用正轨表达式创建URL重写)2006-04-2620:22|AXii
哈哈哈,测试后1publicvoidRewrite_BeginRequest(objectsender,System.EventArgsargs)
2{
3stringappPath=HttpContext.Current.Request.ApplicationPath;
4HttpContext.Current.Response.Write(appPath+"<br/>");
5
6stringstrPath=HttpContext.Current.Request.Url.AbsolutePath;
7HttpContext.Current.Response.Write(strPath+"<br/>");
8
9strPath=strPath.Substring(appPath.Length);
10
11HttpContext.Current.Response.Write(strPath+"<br/>");
12
13UrlRedirectionoPR=newUrlRedirection();
14
15stringstrURL=strPath;
16
17stringstrRewrite=oPR.GetMatchingRewrite(strPath);
18
19if(!String.IsNullOrEmpty(strRewrite))
20{
21strURL=strRewrite;
22}
23else
24{
25strURL=strPath;
26}
27
28HttpContext.Current.RewritePath("~"+strURL);
29}发明这个处置举措关于假造路径会呈现转发毛病,注重第2、3、9行,是我增添的,能够无效的办理假造路径成绩。
2、没法满意页面回发的成绩!怎样办理,还请您来修正:):
刚刚打开这篇专题,猛然见到HAL9000发表的《对于大型公司项目平台选择j2ee的几层认识》系列,深受启发。 |
|