Url Rewriters
12 November 2008 Tips  Marek Sienkiewicz
Bubble
If like me you decide to use Friendly URLs you will at some point need to download and inplement a URL Rewriter.
 
For example say you have a blog article called Url Rewriter,  passing title as the parameter in the querystring the url would be
 
http://www.yoursite.com/article.aspx?Title= Url%20Rewriter.
 
Converting this 'dirty' url to the more friendly
 
http://www.yoursite.com/article/url-rewriter.aspx
 
will result in problems when trying to locate the article in the database. The title field actually holds Url Rewriter and not url-rewriter and therfore the article will not be found.
 
The open source URL Rewriter is avalilable for download from
 
 
Simply download the source files and follow the online instructions. In all probability you will need to create and register a Custom Transform.
 
To create a custom transform class to transform hyphens back to spaces right click App_Code > Add New Item > Class. Name the class something like HyphenToSpace and choose vb as the language. Copy and paste the code below:
 
Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports Intelligencia.UrlRewriter.Utilities

Namespace Intelligencia.UrlRewriter
 
    Public Class HyphenToSpace
        Implements IRewriteTransform
 
        Public Function ApplyTransform(ByVal input As String) As String Implements Intelligencia.UrlRewriter.IRewriteTransform.ApplyTransform  
          
            Return input.Replace("-", " ")
        End Function
 
        Public ReadOnly Property Name() As String Implements Intelligencia.UrlRewriter.IRewriteTransform.Name 
            Get
 
                Return "transform"
 
            End Get
 
        End Property
 
    End Class

End Namespace
 
To register the transform copy and paste the code below to your web.config file:
 
<rewriter>    
<registertransform= "Intelligencia.UrlRewriter.HyphenToSpace,App_Code" />
</rewriter>
 
Then set the rule:
 
<rewrite url="~Article/(.+).aspx" to "~/Article.aspx?Title=${transform($1)}" />
 
Note that the tilde (~) only works with sever controls





Please post your comments:

Name

Email
                           

   

Enter the text you see in the box:

Captcha Image