Microsoft Knowledge Base Email Alertz

Introduction to URL rewriting, to URL mapping, and to URL routing in ASP.NET in a .NET Framework environment

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 976111 - Last Review: October 8, 2009 - Revision: 1.0

Introduction to URL rewriting, to URL mapping, and to URL routing in ASP.NET in a .NET Framework environment

On This Page

INTRODUCTION

When you use Microsoft ASP.NET to develop a Web application, a page may be moved from one directory to another directory. Or, you may map a URL to a page that does not exist. When you try to access this page in a Web browser, you receive an error message that states that the page does not exist.

This article describes how to avoid this problem by using the URL Rewriting Engine to perform dynamic URL rewriting, dynamic URL mapping, and dynamic URL routing.

MORE INFORMATION

The URL Rewriting Engine is a sample project that you can download from the Microsoft Download Center. Generally, URL rewriting and URL mapping are used to map a virtual page to a real page. But where URL rewriting is used to map multiple pages according to one rule, URL mapping is used to map a single page.

By using URL routing, the URL is not changed when an incoming request is handled, because URL routing can extract values from the URL. When you want to create a URL, you can pass parameter values to a method that generates the URL for you.

Usage examples

URL rewriting

To perform URL rewriting, follow these steps:
  1. Download the URL Rewriting Engine by visiting the following Microsoft Web site:
    http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi (http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/msdnurlrewriting.msi)
  2. Run the MSDNURLRewriting.msi file. Some source code files will be installed.
  3. Open the URLRewriter solution in Microsoft Visual Studio, and then build the solution. The URLRewriter.dll file is generated in the debug folder.
  4. Create a new Web application project, and then add to your project a reference to the URLRewriter.dll file.
  5. Modify the Web.config file to set up URL rewriting. For example, you can set up URL rewriting by adding the following tags to the Web.config configuration file:
    <configSections>
    	<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
    </configSections>
    
      <RewriterConfig>
        <Rules>
          <!-- Rules for Blog Content Displayer -->
          <RewriterRule>
            <LookFor>~/Default.aspx</LookFor>
            <SendTo>~/SendToPage.aspx</SendTo>
          </RewriterRule>
        </Rules >
       </RewriterConfig>
    
For more information, visit the following Microsoft Web site:
URL Rewriting in ASP.NET (http://msdn.microsoft.com/en-us/library/ms972974.aspx)

URL mapping

You can use the <urlMappings> tag in the Web.config file to set up URL mapping.

This method is shown in the following code example:
<?xml version="1.0" ?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <system.web>

    <urlMappings enabled="true">
      <add
          url="~/Category.aspx"
          mappedUrl="~/Default.aspx?category=default" />
      <add
          url="~/Autos.aspx"
          mappedUrl="~/Default.aspx?category=autos" />
      <add
          url="~/Games.aspx"
          mappedUrl="~/Default.aspx?category=games" />
      <add
          url="~/Health.aspx"
          mappedUrl="~/Default.aspx?category=health" />
      <add
          url="~/News.aspx"
          mappedUrl="~/Default.aspx?category=news" />
    </urlMappings>

  </system.web>
</configuration>
For more information about URL mapping, visit the following Microsoft Web site:
urlMappings Element (ASP.NET Settings Schema) (http://msdn.microsoft.com/en-us/library/ms228302.aspx)

URL routing

The following code example shows how to set up URL routing:
  public static void RegisterRoutes(RouteCollection routes)
{
  routes.Add(new Route
  (
     "Category/{action}/{categoryName}"
          new CategoryRouteHandler()
  )
    {
       Defaults = new RouteValueDictionary 
           {{"categoryName", "food"}, {"action", "show"}}
     }
  );
}
        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }
} 
For more information about URL routing, visit the following Microsoft Web sites:
ASP.NET MVC Framework (Part 2): URL Routing (http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx)
ASP.NET Routing (http://msdn.microsoft.com/en-us/library/cc668201.aspx)

APPLIES TO
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 2.0
  • Microsoft .NET Framework 3.0
  • Microsoft .NET Framework 3.5
Keywords: 
kbexpertiseadvanced kbhowto kbsurveynew kbinfo KB976111
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate