hi all
following tutorial describes simple solution to implement URL rewriting solution in asp.net.
i'll describe this in step by step walk through:
1- include "ThunderMain.URLRewriter.dll" in your bin directory. It is black box file which helps you to perform server side redirect to your required location.
This dll and sample code include with this post as attachment2- Past following code in your Global.asax
Code:
protected void Application_BeginRequest(Object sender, EventArgs e){
ThunderMain.URLRewriter.Rewriter.Process();
}
3- Create a class file with the name Rewriter.CS with following code
Code:
using System;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.Configuration;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Xml.Xsl;
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile(@"keyfile.snk")]
[assembly: AssemblyKeyName("")]
[assembly: AssemblyVersion("1.0.783.30976")]
namespace ThunderMain.URLRewriter {
public class Rewriter : IConfigurationSectionHandler {
protected XmlNode _oRules=null;
protected Rewriter(){}
public string GetSubstitution(string zPath) {
Regex oReg;
foreach(XmlNode oNode in _oRules.SelectNodes("rule")) {
oReg=new Regex(oNode.SelectSingleNode("url/text()").Value);
Match oMatch=oReg.Match(zPath);
if(oMatch.Success) {
return oReg.Replace(zPath,oNode.SelectSingleNode("rewrite/text()").Value);
}
}
return zPath;
}
public static void Process() {
Rewriter oRewriter=(Rewriter)ConfigurationSettings.GetConfig("system.web/urlrewrites");
string zSubst=oRewriter.GetSubstitution(HttpContext.Current.Request.Path);
if(zSubst.Length>0) {
HttpContext.Current.RewritePath(zSubst);
}
}
#region Implementation of IConfigurationSectionHandler
public object Create(object parent, object configContext, XmlNode section) {
_oRules=section;
// TODO: Compile all Regular Expressions
return this;
}
#endregion
}
}
4- Past following code in your default code behind page or where u want to show actual url ( used for testing only )
Code:
static int x=1;
private void Page_Load(object sender, System.EventArgs e) {
Response.Write("Page= <b>" + Request.Url + "</b><br> called " + x + " times" );
x++;
}
5- include following code in web.config configuration section
Code:
configuration>
<configSections>
<sectionGroup name="system.web">
<section name="urlrewrites" type="ThunderMain.URLRewriter.Rewriter, ThunderMain.URLRewriter, Version=1.0.783.30976, Culture=neutral, PublicKeyToken=7a95f6f4820c8dc3"/>
</sectionGroup>
</configSections>
6- now you are almost done! just write you rewriting rules in your web.config like below;
Code:
<urlrewrites>
<rule>
<url>/urlrewriter/show\.asp</url>
<rewrite>show.aspx</rewrite>
</rule>
<rule>
<url>/urlrewriter/wohs\.asp</url>
<rewrite>show.aspx</rewrite>
</rule>
<rule>
<url>/urlrewriter/show/(.*).asp</url>
<rewrite>show.aspx?$1</rewrite>
</rule>
<rule>
<url>/urlrewriter/(.*)show\.html</url>
<rewrite>show.aspx?id=$1&cat=4</rewrite>
</rule>
<rule>
<url>/urlrewriter/s/h/o/w/(.*)\.html</url>
<rewrite>/urlrewriter/show.aspx?id=$1</rewrite>
</rule>
<rule>
<url>/urlrewriter/s/(.*)\.html</url>
<rewrite>/urlrewriter/show.aspx?id=$1</rewrite>
</rule>
<rule>
<url>/urlrewriter/Cables/(.*)\.html</url>
<rewrite>/urlrewriter/show.aspx?id=$1</rewrite>
</rule>
</urlrewrites>
File Attachment(s):
urlrewriter.rar (9kb) downloaded 47 time(s).