标签:des style class blog code java
原文:Asp.net实现URL重写
【概述】
URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程。重写URL是非常有用的一个功能,因为它可以让你提高搜索引擎阅读和索引你的网站的能力;而且在你改变了自己的网站结构后,无需要求用户修改他们的书签,无需其他网站修改它们的友情链接;它还可以提高你的网站的安全性;而且通常会让你的网站更加便于使用和更专业。
1 class MyUrlWriter : IHttpModule 2 { 3 public void Init( HttpApplication context) 4 { 5 context.BeginRequest += new EventHandler (context_BeginRequest); 6 } 7 8 protected void context_BeginRequest( object sender, EventArgs e) 9 { 10 HttpApplication application = sender as HttpApplication ; 11 HttpContext context = application.Context; //上下文 12 string url = context.Request.Url.LocalPath; //获得请求URL 13 14 Regex articleRegex = new Regex ("/Article/[A-Z0-9a-z_]+" ); //定义规则 15 if (articleRegex.IsMatch(url)) 16 { 17 string paramStr = url.Substring(url.LastIndexOf(‘/‘ ) + 1); 18 context.RewritePath( "/Article.aspx?id=" + paramStr); 19 } 20 else 21 { 22 context.RewritePath( "/Default.aspx" ); 23 } 24 } 25 26 public void Dispose() { } 27 }
1 <httpModules > 2 <add name = "UrlReWriter " type =" UrlReWriter.MyUrlWriter,UrlReWriter " /> 3 </httpModules >
1 public partial class Article : System.Web.UI.Page 2 { 3 protected void Page_Load( object sender, EventArgs e) 4 { 5 Response.Write(Request.QueryString[ "id" ]); 6 } 7 }
1 <a href ="/Article/35">测试url重写</a>
1 /// <summary> 2 /// Provides a rewriting HttpModule. 3 /// </summary> 4 public class ModuleRewriter : BaseModuleRewriter 5 { 6 /// <summary> 7 /// This method is called during the module‘s BeginRequest event. 8 /// </summary> 9 /// <param name="requestedRawUrl"> The RawUrl being requested (includes path and querystring).</param> 10 /// <param name="app"> The HttpApplication instance. </param> 11 protected override void Rewrite( string requestedPath, System.Web.HttpApplication app) 12 { 13 // log information to the Trace object. 14 app.Context.Trace.Write( "ModuleRewriter" , "Entering ModuleRewriter"); 15 16 // get the configuration rules 17 RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules; 18 19 // iterate through each rule... 20 for (int i = 0; i < rules.Count; i++) 21 { 22 // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) 23 string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$" ; 24 25 // Create a regex (note that IgnoreCase is set...) 26 Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); 27 28 // See if a match is found 29 if (re.IsMatch(requestedPath)) 30 { 31 // match found - do any replacement needed 32 string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath,re.Replace(requestedPath, rules[i].SendTo)); 33 34 // log rewriting information to the Trace object 35 app.Context.Trace.Write( "ModuleRewriter" , "Rewriting URL to " + sendToUrl); 36 37 // Rewrite the URL 38 RewriterUtils.RewriteUrl(app.Context, sendToUrl); 39 break ; // exit the for loop 40 } 41 } 42 43 // Log information to the Trace object 44 app.Context.Trace.Write( "ModuleRewriter" , "Exiting ModuleRewriter"); 45 } 46 }
1 public partial class Article : System.Web.UI.Page 2 { 3 protected void Page_Load( object sender, EventArgs e) 4 { 5 Response.Write(Request.QueryString[ "id" ]); 6 } 7 }
public partial class News : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { Response.Write( string .Format("日期:{0}<br/>" , Request.QueryString["date" ])); Response.Write( string .Format("ID:{0}<br/>" , Request.QueryString["id" ])); } }
1 <configuration> 2 <configSections> 3 <section name = "RewriterConfig " type =" URLRewriter.Config.RewriterConfigSerializerSectionHandler,URLRewriter " /> 4 </configSections> 5 </configuration>
1 <httpHandlers> 2 <remove verb = "* " path = "*.asmx " /> 3 <add verb = "* " path = "* " type = "URLRewriter.RewriterFactoryHandler, URLRewriter" /> 4 <add verb = "* " path = "*.html " type =" URLRewriter.RewriterFactoryHandler, URLRewriter " /> 5 </httpHandlers>
1 <configuration> 2 <RewriterConfig> 3 <Rules> 4 <RewriterRule> 5 <LookFor>~/Article/([A-Z0-9a-z_]+) </LookFor> 6 <SendTo>~/Article.aspx?id=$1 </SendTo> 7 < RewriterRule> 8 <RewriterRule> 9 <LookFor>~/News/(\d{4}-\d{2}-\d{2})/(\d{1,6})\.html? </LookFor> 10 <SendTo>~/News.aspx?date=$1 & id=$2</SendTo> 11 </RewriterRule> 12 </Rules> 13 </RewriterConfig> 14 </configuration>
1 <a href ="<% = ResolveUrl("Article/35")%> "> 测试文章url重写</a>< br /> 2 <a href ="<% = ResolveUrl("News/2014-06-11/285864.html")%> "> 测试新闻url重写</a>
标签:des style class blog code java
原文地址:http://www.cnblogs.com/lonelyxmas/p/3782230.html