Webmaster Forums Banner Professional Hosting from Just Host
Welcome Guest Search | Active Topics | Members | Log In | Register

URL Rewriting in ASP.NET : Tutorial Options
ca4nul
Posted: Thursday, May 29, 2008 8:08:46 AM

Rank: Advanced Member
Groups: Member

Joined: 11/9/2007
Posts: 223
Points: 575
Location: UK
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 attachment

2- 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&amp;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).


Sponsor
Posted: Thursday, May 29, 2008 8:08:46 AM
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by HaqTech.Com
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.525 seconds.