Perform URL rewriting in ASP. NET (1)

Source: Internet
Author: User
Introduction
Common URL rewriting usage
What will happen when the request arrives at iis?
Implement URL rewriting
Build a URL rewriting Engine
Use the URL rewriting engine to perform simple URL rewriting
Create a URL with "deletable"
Conclusion
References

Introduction
Let's take a moment to look at some URLs on the website. Have you found any image similar to aspx? Empid = 459-099 & type = Summary "> http://yoursite.com/info/dispEmployeeInfo.aspx? Empid = 459-099 & type = Summary URL? Alternatively, you may move a series of web pages from one directory or website to another directory or website. As a result, visitors who have used the old URL as a bookmarks will be disconnected. In this article, we will learn how to change the aspx? Empid = 459-099 & type = Summary "> http://yoursite.com/info/dispEmployeeInfo.aspx? Empid = 459-099 & type = summary is replaced with a URL similar to a http://yoursite.com/people/sales/chuck.smith, using URL rewriting to abbreviated lengthy URLs as rich meaningful and easily remembered URLs. We will also learn how to rewrite a URL to create a smart 404 error.

URL rewriting is the process of intercepting incoming Web requests and automatically redirecting requests to other resources. During URL rewriting, the request is usually checked and redirected to another URL Based on the URL value. For example, when a website is reorganized and all the webpages in the/people/directory are moved to the/INFO/employees/directory, you may want to use URL rewriting to check whether the Web request points to a file in the/people/directory. If the request points to a file in the/people/directory, you may want to automatically redirect the request to the same file in the/INFO/employees/directory.

Using traditional ASP, the only way to rewrite a URL is to write an ISAPI filter or buy a third-party product that provides the URL rewriting function. However, Microsoft & reg; ASP. NET allows you to easily create your own URL rewriting software in many ways. This article discusses various technologies that can be used by ASP. NET developers to rewrite URLs, and then discusses some actual use cases of URL rewriting. Before going into the technical details of URL rewriting, let's take a look at some daily scenarios that can be rewritten using URL.

Back to Top
Common URL rewriting usage
When creating a data-driven ASP. NET Website, a single web page is usually generated, which displays a subset of Database Data Based on query string parameters. For example, when designing an e-commerce site, one of your tasks is to allow users to browse products for sale. To this end, you can create a page named displaycategory. aspx that displays products of a given category. You can query string parameters to specify products of the specified type. That is to say, if you want to browse the widget products for sale and the categoryid of all widget products is 5, you can visit the following url: aspx? Categoryid = 5 "> http://yousite.com/displayCategory.aspx? Categoryid = 5.

There are two shortcomings in creating a website with such a URL: first, from the end user's perspective, URL aspx? Categoryid = 5 "> http://yousite.com/displayCategory.aspx? Categoryid = 5 is messy. Availability expert Jakob Neilsen recommends that you select a URL based on the following criteria:

• Brief.

• Easy to type.

• You can see the structure of the site.

• "Deletable" allows users to browse the site by deleting the URL components.

I also want to add a standard, that is, the URL should be easy to remember. URL aspx? Categoryid = 5 "> http://yousite.com/displayCategory.aspx? Categoryid = 5 does not comply with any Neilsen standards and is not easy to remember. It is required that you enter the query string value, which makes it very difficult to type the URL, in addition, only experienced web developers who know how to query string parameters and their name/value pairs can delete URLs ".

A better way is to allow the use of realistic and easily remembered URLs, such as http://yoursite.com/products/widgets. With a look at the url, You can infer the content to be displayed-information about the widget. This URL is also easy to remember and share. I can tell my colleague, "Please check yoursite.com/products/widgets,they can open the page without asking me what the URL is. (For a try, you only need to name the "Amazon.com page !) This URL will also be displayed, and it should be "deletable. That is to say, if you delete the end of the URL, Type.

Note: To obtain the best example of a URL that can be deleted, consider using a URL generated by many blog engines. To view post for January 28, 2004, users can access URLs such as http://someblog.com/2004/01/28. If the URL is deleted as http://someblog.com/2004/01,the user will see all the posts in July January 2004. Delete the URL further to http://someblog.com/2004 to display all posts for 2004.

In addition to simplified URLs, URL rewriting is often used to process website restructuring, so as to avoid many links being broken or bookmarks being expired.

Back to Top
What will happen when the request arrives at iis?
Before formally studying how to rewrite a URL, it is important to know how Microsoft & reg; Internet Information Services (IIS) processes incoming requests. When a request arrives at the IIS web server, IIS checks the extension of the requested file to determine how to process the request. IIS can handle requests (such as HTML pages, images, and other static content) by itself, or route requests to ISAPI extensions. (ISAPI extension is an unmanaged compilation class that processes incoming Web requests. The task is to generate the content of the requested resource .)

For example, when a request is sent to the info. ASP Webpage, IIS routes the message to the ASP. dll ISAPI extension. Then, the ISAPI extension loads the requested ASP page, executes the page, returns the rendered HTML to IIS, and IIS sends the HTML back to the request client. For ASP. NET pages, IIS routes the message to the aspnet_isapi.dll ISAPI extension. Then, the aspnet_isapi.dll ISAPI extension passes the processing operation to the managed ASP. NET auxiliary process.ProgramThe request is processed and the HTML of the ASP. NET webpage is returned.

You can customize IIS to specify the ing between the extension and ISAPI extension. Figure 1 shows the "Application configuration" dialog box of the Internet information services management tool. Note that extensions related to ASP. NET (. aspx, ascx, config, asmx, REM, Cs, VB, and others) have been mapped to the aspnet_isapi.dll ISAPI extension.

 

Figure 1. configured file extension ing

This article discusses how IIS manages incoming requests a little beyond the scope of this article. However, you canArticleInside IIS and ASP. NET find an in-depth discussion of this content. The ASP. net engine only processes incoming Web requests whose extensions have been explicitly mapped to aspnet_isapi.dll in IIS. This is important to understand.

Use ISAPI filter to check requests
In addition to ing the file extension of the incoming Web request to the corresponding ISAPI extension, IIS also executes many other tasks. For example, IIS will try to authenticate the requesting user and determine whether the authenticated user has the permission to access the requested file. During the validity period of the request, IIS will go through several statuses. In each status, IIS triggers events that can be processed programmatically using the ISAPI filter.

Like ISAPI extensions, ISAPI filters are hosted on Web servers.CodeBlock. ISAPI extensions are designed to respond to requests for specific file types. On the other hand, the ISAPI filter also contains code that can respond to events caused by IIS. The ISAPI filter can intercept or even modify incoming and outgoing data. ISAPI filters can be applied in many aspects, including:

• Authentication and authorization.

• Record and monitor.

• HTTP compression.

• URL rewriting.

Although ISAPI filters can be used to perform URL rewriting, this article will discuss how to use ASP. NET to implement URL rewriting. However, we will weigh the use of ISAPI filters against the use of technology in ASP. NET to implement URL rewriting.

What will happen when a request enters the ASP. NET engine?
Before ASP. NET, you need to use the ISAPI filter to rewrite the URL on the IIS web server. Because the ASP. NET engine is very similar to IIS, you can use ASP. NET for URL rewriting. The similarities exist because the ASP. NET engine can implement the following functions:

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.