[Translation] url ing in asp.net 2.0

Source: Internet
Author: User
Address: http://aspnet.4guysfromrolla.com/articles/011007-1.aspx
[Source code download]

Url ing in asp.net 2.0

Original article released on: 2007.01.10
By Scott Mitchell
Translation: webabcd

Introduction
Url ing is a new feature of asp.net 2.0. It allows developers to map one page to another. If a url request comes in, the program will automatically remap it to the server. For example, if you configure ~ /Beverages. aspx ing ~ /ProductsByCategory. aspx? CategoryID = 1 & CategoryName = Beverages? CategoryID = 1 & CategoryName = Beverages. What this user sees in the address bar of the browser is http://yoursite.com/beverages.aspx. They do not know that this request has been remapped.

Url ing is often used to provide friendly URLs, which are easier to read and understand. For example, Beverages. aspx is better than ProductsByCategory. aspx? CategoryID = 1 & CategoryName = Beverages is easier to understand. When you modify the site structure, url ing is also very useful. Imagine that all the product information needs to be renewed.

In this article, we will study how to set the url ing function provided by the asp.net 2.0 web application, and also take a look at how url ing works. We will also discuss the implementation technology of url ing in asp.net 1. x and the extension of url ing in asp.net 2.0. Next you will see more.

Define url ing
It is very easy to use url ing in asp.net 2.0. We can set it directly in web. config. Through some simple configurations, we can create url ing functions in some scenarios, but it is not applicable to some complex scenarios, such as the need to dynamically define ing or make it more adaptive. This article will provide a simple example later.

To specify a ing, we can add a <urlMappings> element in web. config, set its enabled attribute to true, and map each <add> element. In the <add> element, specify the url (introduced url) and mappedUrl (url to be mapped) attributes.

The example download is provided at the end of this article. It is a web application of asp.net 2.0 and uses the Northwind database to display product information. The ProductsByCategory. aspx page requires two parameters: CategoryID and CategoryName. Then, this page uses a Label control to display the CategoryName, A GridView control, and a SqlDataSource control to display the list of products that CategoryID points. Therefore, the user uses the address ~ /ProductsByCategory. aspx? CategoryID = 1 & CategoryName = Beverages to access this page.

We can use the url ing feature of asp.net 2.0 to provide users with a friendly url, such ~ /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, you can use the address ~ /ProductsByCategory. aspx? CategoryID = 1 & CategoryName = Beverages or ~ /Beverages. aspx. <Configuration>

<System. web>

<UrlMappings enabled = "true">
<Add
Url = "~ /Beverages. aspx"
MappedUrl = "~ /ProductsByCategory. aspx? CategoryID = 1 & amp; CategoryName = Beverages "/>
<Add
Url = "~ /Condiments. aspx"
MappedUrl = "~ /ProductsByCategory. aspx? CategoryID = 2 & amp; CategoryName = Condiments "/>
<Add
Url = "~ /Confections. aspx"
MappedUrl = "~ /ProductsByCategory. aspx? CategoryID = 3 & amp; CategoryName = Confections "/>
<Add
Url = "~ /Dairy. aspx"
MappedUrl = "~ /ProductsByCategory. aspx? CategoryID = 4 & amp; CategoryName = Dairy + Products "/>
<Add
Url = "~ /Grains. aspx"
MappedUrl = "~ /ProductsByCategory. aspx? CategoryID = 5 & amp; CategoryName = Grains + and + Cereals "/>
<Add
Url = "~ /Meat. aspx"
MappedUrl = "~ /ProductsByCategory. aspx? CategoryID = 6 & amp; CategoryName = Meat + and + Poultry "/>
<Add
Url = "~ /Produce. aspx"
MappedUrl = "~ /ProductsByCategory. aspx? CategoryID = 7 & amp; CategoryName = Produce "/>
<Add
Url = "~ /Seafood. aspx"
MappedUrl = "~ /ProductsByCategory. aspx? CategoryID = 8 & amp; CategoryName = Seafood "/>
</UrlMappings>
</System. web>
</Configuration>

Note: Because web. config is in xml format, all xml features are retained. For example, if you want to enter the Symbol &, you must write it as & amp.

When this page is accessed through a friendly url (Beverages. aspx), parameters in the ing file are used. Through the following we can notice that the user entered Beverages. aspx in the address bar of the browser, but after this request is sent to the server, it will be automatically mapped ~ /ProductsByCategory. aspx? CategoryID = 1 & CategoryName = Beverages, but what you see in the address bar is still Beverages. aspx. However, using the server code, the CategoryID and CategoryName parameters are used to display the product list under the category name and category.

How does asp.net 2.0 map URLs?
When a request arrives at the web server, asp.net runtime determines the requested url, and then calls the corresponding class of the page to display the result. Each page has its own lifecycle, including control initialization and processing. After some stages, such as PreInit, Init, and Load, the processing result is displayed in the tag, then, pass these tags to the browser for display.

Before these steps, the system first checks the requested url and checks whether the requested url appears in the url ing file. If yes, use the RewritePath method of the HttpContext class. This method will refine the url of the current request through the rules specified in web. config. After the information is updated, the system will replace the original url entered by the user with the mapped url. However, what you see in the address bar of the browser is the original url.

The workflow diagram is as follows:

Disadvantages of url ing in asp.net 2.0.
There are two main disadvantages of url ing in asp.net 2.0. First, all url mappings must be statically defined in web. config. This makes it unrealistic for some data-driven web sites to manually enter all relevant information into the configuration file if they want to use url ing. There is an example after this article. You will find that if you delete a category from the database or add a category, you must manually modify the web. config File, delete or add a <add> element at the <urlMappings> node.

Another disadvantage is that regular expressions cannot be used. When a large number of URLs need to be mapped, one scenario is to map all URLs by defining a ing mode, this special requirement is very useful in site reconstruction. If you can use regular expressions in the rewrite rules, a simple ing can map all old folders to new folders. (Assume that a web site renames the "Old" folder as "New".) <add url = "~ /Old/(. *) "mappedUrl = "~ /New/$1 "/>

During the period when asp.net 2.0 cannot provide the regular expression function during the implementation of url ing, Chris Pietschmann provides an HTTP Module code to do this, in his Blog, the HTTP Module "asp.net 2.0: providing regular expression for url ing" can also dynamically write ing rules.

On the other hand, there is also a small drawback, that is, after the user accesses the page through url ing, if <form> is submitted, it will be transferred to the real page. That is to say, if the user accesses ProductsByCategory. aspx through the friendly url Beverages. aspx? CategoryID = 1 & CategoryName = Beverages page, if the page sends data back, the browser will request ProductsByCategory. aspx? CategoryID = 1 & CategoryName = Beverages instead of Beverages. aspx. The function works normally, but the address on the address bar changes. This will confuse users who access the page by writing down the url address.

Url ing in asp.net 1. x
The <urlMappings> element similar to asp.net 2.0 is not provided in asp.net 1.x, but the ing function can also be implemented through custom HTTP Handler or Module. For detailed implementation process, 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. Chris Pietschmann used the same idea for the HTTP Module that provided regular expressions for url ing written for asp.net 2.0.

The article "url rewriting in asp.net" uses the form-based re ing to friendly URLs after submission.

Conclusion
In this article, we will take a look at the url ing feature in asp.net 2.0. It provides a very simple method to implement url ing. This technology is usually used to create friendly URLs and process site refactoring. Unfortunately, we need to manually create url ing items. If we need to dynamically create url ing, we can implement it through custom HTTP Module.

Happy Programming

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.