REST Friendly URLs

9 ביוני 2008

תגיות: , , ,
4 תגובות

REST Friendly URLs


Lest say you have an articles site, which have:



  • Category: credit-card
  • Sub Category: student
  • Article Id: students-heed-parents-on-credit-card-advice

The following two samples describes the difference between friendly URLs and Dirty URLs:


Dirty URLs


http://…/article/Article.aspx?categoryId=credit-card&subCategoryId=student&articleId=student-article

http://…/Category.aspx?categoryId=credit-card

http://…/SubCategory.aspx?categoryId=credit-card&subCategoryId=student


Friendly URLs


http://…/article/credit-card/student/student-article

http://…/category/credit-card

http://…/category/credit-card/student

Why Does URL Rewriting Matter?


There are some reasons why me matter about rewriting URLs to get a clean version of them:



  • Bookmaking – The URLs are part of your API in REST world, and such, client may bookmarks them or have some client programs that works with your API links. Your goal is to stay with the same URL no matter if you changing tomorrow your technology.
  • Usability – Friendly URLs are easy to remember and provides cues to the user about the resource itself.
  • Hide implementation details – The main reason is abstraction. The user doesn't require to know how I implemented it.
  • Security – The query string which follow the question mark (?) is often modified by hackers.
  • Logical URLs – If you have a site with 400,000 pages, it is not easy to maintain all of them, for example, you need to add some script for all pages…, using logical you can use only few physical pages to serve all the pages Logical URLs means that any URL in your site represents resource which is not physical but logical.
  • Search Engine Optimization (SEO) – When search engine gets URL which imply that the underlying site is dynamic, and use query parameters, it treats to him different than he gets a friendly URL.

URL Rewriting with IIS 7.0


Intercepting request with IIS 7.0 is very easy:



  • Implement HttpModule
  • Configure it in Web.Config

The following sample is show how to remove '.svc' extension from WCF service hosted in IIS:


 


    public void Init(HttpApplication app)


    {


        app.BeginRequest += delegate


        {


            HttpContext ctx = HttpContext.Current;


            String path = ctx.Request.AppRelativeCurrentExtensionFilePath;


            // …


            ctx.RewritePath(path + “.svc”, …);   


        }


    }


URL Rewriting with UrlRewriter.NET




    <section


      name="rewriter"


      requirePermission="false"


      type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>


  </configSections>


 



      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>


    </httpModules>


 



      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule"/>


    </modules>



  • Define your mapping:


  <rewriter>


    <rewrite


        url="~/article/(.+)/(.+)/(.+)"


        to="~/Article.aspx?categoryId=$1&amp;subCategoryId=$2&amp;articleId=$3"/>


    <rewrite


        url="~/category/(.+)/(.+)"


        to="~/SubCategory.aspx?categoryId=$1&amp;subCategoryId=$2"/>


    <rewrite


        url="~/category/(.+)"


        to="~/Category.aspx?categoryId=$1"/>


  </rewriter>


</configuration>



  • The source code for Article.aspx.cs is show in the following sample:

 



        protected void Page_Load(object sender, EventArgs e)


        {


            ASP.article_aspx page = sender as ASP.article_aspx;


            if (page == null)


            {


                return;


            }


     


            String categoryId = page.ApplicationInstance.Request.Params["categoryId"];


            String subCategoryId = page.ApplicationInstance.Request.Params["subCategoryId"];


            String articleId = page.ApplicationInstance.Request.Params["articleId"];


Conclusion


Friendly URLs are simple to implements, and have a big rewards on this few additional lines of code. I show here two methods to use it, there are few more (ISAPI filters on IIS 6.0, HttpContext.RewritePath() in ASP.NET), but I wanted to concentrate on the better approaches.


With Friendly URLs your site links (and API) are clean, easy to remember, SEO friendly, and has abstraction level.


kick it on DotNetKicks.com



Technorati Tags: ,,,

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

4 תגובות

  1. Mahdi Taghizadeh6 ביולי 2008 ב 0:09

    I think you've forgotten to mention in in Web.Config file. It is required when you wanna rewrite extensionless URLs in ASP.NET.

    להגיב
  2. Jim1 בנובמבר 2009 ב 0:52

    The best way to spend free time is to spend time with sweet escort models from Busty escorts. That escort industry is greatly in demand in London.

    להגיב
  3. elitevipmodels98@yahoo.com15 באוגוסט 2012 ב 11:35

    Excellent post, looking forward to reading more from you.

    להגיב