URL Mappings in asp.net 2.0

Source: Internet
Author: User
Tags config expression regular expression
asp.net introduction
URL mapping is a new feature of ASP.net 2.0, which allows developers to map a page to another URL. If a URL request comes in, the program will automatically remap it to the server. For example, if you configure a ~/beverages.aspx map to ~/productsbycategory.aspx in a program? Categoryid=1&categoryname=beverages, then when the user enters the http://YourSite.com/Beverages.aspx in the browser, the server will automatically go to http://when it is received. Yoursite.com/productsbycategory.aspx? Categoryid=1&categoryname=beverages to deal with. The user still sees the http://YourSite.com/Beverages.aspx in the browser's address bar. They will not know that the request has been remap.

URL mappings are often used to provide friendly URLs, and the friendly URLs are easier to read and more understandable. Like beverages.aspx than productsbycategory.aspx? Categoryid=1&categoryname=beverages easier to understand. URL mapping is also useful when you modify the site structure. Imagine that all product information needs to be http://YourSite.com/Products/... To access, if you want to modify this structure, through http://YourSite.com/Catalog/... Instead, if one of your old users accesses the Favorites folder, you get a 404 error, and if you use URL mapping, two URLs can be written to access the product information.

In this article, we'll look at how to set the URL mapping feature provided by the ASP.net 2.0 Web application and see how URL mapping works. We will also discuss the implementation of URL mapping in ASP.net 1.x, and extend the URL mapping in asp.net 2.0. Next you will see more.


Defining URL Mappings
Using URL mappings in asp.net 2.0 is very simple and can be set directly in Web.config. With some simple configuration, we can create URL mapping in some scenarios, but it does not apply to complex scenarios, such as the need to dynamically define mappings or require stronger adaptability. A simple example will be provided later in this article.

To specify a mapping, we can add a <urlMappings> element to the Web.config and set its Enabled property to True, and then map it in each <add> element. In the <add> element, specify the URL (the introduced URL) and the Mappedurl (the URL to map to) attribute, respectively.

A sample download is provided at the end of this article, which is a asp.net 2.0 Web application that uses the Northwind database to display product information. The Productsbycategory.aspx page requires two parameters, namely CategoryID and CategoryName. The page then displays CategoryName with a label control, a GridView control, and a SqlDataSource control to display a list of the products that CategoryID points to. So, the user uses the address ~/productsbycategory.aspx? Categoryid=1&categoryname=beverages to visit this page.

We can use the URL mapping feature of ASP.net 2.0 to provide users with a friendly URL, like ~/beverages.aspx. We write 8 mappings in the <urlMappings> element to create a friendly URL for each product class in the Northwind database. After writing these mappings, if you want to see the beverage products can be through the address ~/productsbycategory.aspx? Categoryid=1&categoryname=beverages or ~/beverages.aspx to visit.
<configuration>

<system.web>

<urlmappings enabled= "true" >
<add
Url= "~/beverages.aspx"
Mappedurl= "~/productsbycategory.aspx?" Categoryid=1&categoryname=beverages "/>
<add
Url= "~/condiments.aspx"
Mappedurl= "~/productsbycategory.aspx?" Categoryid=2&categoryname=condiments "/>
<add
Url= "~/confections.aspx"
Mappedurl= "~/productsbycategory.aspx?" Categoryid=3&categoryname=confections "/>
<add
Url= "~/dairy.aspx"
Mappedurl= "~/productsbycategory.aspx?" Categoryid=4&categoryname=dairy+products "/>
<add
Url= "~/grains.aspx"
Mappedurl= "~/productsbycategory.aspx?" Categoryid=5&categoryname=grains+and+cereals "/>
<add
Url= "~/meat.aspx"
Mappedurl= "~/productsbycategory.aspx?" Categoryid=6&categoryname=meat+and+poultry "/>
<add
Url= "~/produce.aspx"
Mappedurl= "~/productsbycategory.aspx?" Categoryid=7&categoryname=produce "/>
<add
Url= "~/seafood.aspx"
Mappedurl= "~/productsbycategory.aspx?" Categoryid=8&categoryname=seafood "/>
</urlMappings>
</system.web>
</configuration>
Note: Since web.config is in XML format, all XML features will be preserved. For example, if you want to enter a symbol, write A &.

When this page is accessed through a friendly URL (beverages.aspx), the parameters in the mapping file are used. We can note from the screenshot below that the user has entered the beverages.aspx in the address bar of the browser, but when the request is sent to the server, it is automatically mapped to the ~/productsbycategory.aspx? Categoryid=1&categoryname=beverages, and what the user sees in the address bar is still beverages.aspx. However, through the server-side code, parameter CategoryID and CategoryName will be used to render the product list under the category name and category.



ASP.net 2.0 is how the URL is mapped
When a request arrives at the Web server, asp.net runtime decides which URL to request, and then the corresponding class of the calling page renders the result. Each page has its own lifecycle, including the initialization and processing of controls, and the processing results are rendered in markup after some stages such as Preinit,init,load, which are then passed to the browser to display.

Before these steps occur, the URL of the request is checked first, and the requested URL is viewed as appearing in the URL mapping file. If so, the RewritePath method of the HttpContext class is used, which will make the current requested URL more granular through the rules specified in Web.config. When this information is updated, the system will replace the URL that the user originally entered with the mapped URL. But what the user sees in the browser's address bar is still the URL that was originally entered.

Its workflow diagram is as follows:



The disadvantage of URL mapping in asp.net 2.0.
The URL mapping in asp.net 2.0 has two main drawbacks. First, all URL mappings must be statically defined in Web.config. This makes it impractical for some data-driven Web sites to manually enter all relevant information into the configuration file if they want to use URL mapping. After this article concludes with an example, you will find that if you delete a category from the database or add a class, you must manually modify the Web.config file, at the <urlMappings> node to delete or add a <add> element.

Another disadvantage is that you cannot use regular expressions. When there is a large number of URLs that need mapping, one scenario is to map all URLs by defining a mapping pattern, a particular requirement that is useful in the refactoring of some sites. If you can use regular expressions in overriding rules, a very simple mapping can accomplish all the old folders mapped to new folders. (Suppose a Web site renames the "old" folder to "new")
<add url= "~/old/(. *)" mappedurl= "~/new/$1"/>
During the period when ASP.net 2.0 was not able to provide regular expression functionality during the implementation of the URL map, Chris Pietschmann provided a section of HTTP module code to do this, which was described in detail in his blog "asp.net 2.0: Provide regular expression functionality to URL mapping "This HTTP module can also dynamically write mapping rules.

On the other hand, there is a small drawback is that when the user through the URL map to access the page, if the <form> submitted, will go to the real page. That is, if the user accesses the productsbycategory.aspx through a friendly URL beverages.aspx? Categoryid=1&categoryname=beverages page, if the page postback data, then the browser will go to request productsbycategory.aspx? Categoryid=1&categoryname=beverages rather than beverages.aspx. Functions can also be implemented normally, but the address bar addresses the change, which will be by writing down the URL address to access the page of the user confused.


URL mappings in the ASP.net 1.x
A <urlMappings> element similar to ASP.net 2.0 is not available in ASP.net 1.x, but the functionality of the mapping can also be achieved through a custom HTTP handler or module. For a detailed implementation, see my other article, "url rewriting in asp.net." This article was written for ASP.net 1.x, but the idea also applies to ASP.net 2.0. The same idea was used by Chris Pietschmann for the HTTP module written for ASP.net 2.0 to provide regular expression functionality for URL mapping.

URL rewrite in asp.net this article uses a technique that is mapped to a friendly URL after the form is submitted.


Conclusion
In this article, we looked at the features of URL mapping in asp.net 2.0, which provides a very simple way to implement URL mapping. This technique is usually used to create friendly URLs and to handle site refactoring. Unfortunately, we need to create URL mapping entries manually, and if we need to create URL mappings dynamically, we can do that by customizing the HTTP module.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.