DCSIMG
REST Friendly URLs - Gady Elkarif's Blog

Gady Elkarif's Blog

REST Friendly URLs

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: ,,,
פורסם: Jun 09 2008, 02:31 PM by egady | with 6 comment(s) |
תגים:, , ,

תוכן התגובה

DotNetKicks.com כתב/ה:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# June 9, 2008 2:45 PM

Gady Elkarif's Blog כתב/ה:

WCF 3.5 REST Presentation Hi, I want to thank to Moshe, Microsoft people and all of those who come to

# June 9, 2008 2:48 PM

Mahdi Taghizadeh כתב/ה:

I think you've forgotten to mention  <modules runAllManagedModulesForAllRequests="true"> in <system.webserver> in Web.Config file. It is required when you wanna rewrite extensionless URLs in ASP.NET.

# July 6, 2008 12:09 AM

Jim כתב/ה:

The best way to spend free time is to spend time with sweet escort models from <a href="www.baccaratgirls.com/">Busty escorts</a>. That escort industry is greatly in demand in London.

# November 1, 2009 12:52 AM

RhUGTdq כתב/ה:

Hi! RFRfOBB

# December 25, 2009 4:42 PM

elitevipmodels98@yahoo.com כתב/ה:

Excellent post, looking forward to reading more from you.

# August 15, 2012 11:35 AM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 6 and 8 and type the answer here:


Enter the numbers above: