URL rewriting in ASP. NET (5)

Source: Internet
Author: User

<Rewriterconfig>
<Rules>
<! -- Product tabulation rules -->
<Rewriterrule>
<Lookfor> ~ /Products/beverages \. aspx </lookfor>
<Sendto> ~ /Listproductsbycategory. aspx? Categoryid = 1 </sendto>
</Rewriterrule>
<Rewriterrule>
</Rules>
</Rewriterconfig>

As you can see, this rule will be searched to check whether the path of the user request is/products/beverages. aspx. If yes, it will rewrite the URL to/listproductsbycategory. aspx? Categoryid = 1.

Note: The <lookfor> element escapes the periods in beverages. aspx. This is because the <lookfor> value is used in the regular expression mode and the period is a special character in the regular expression. This character indicates "matching any character". For example, match with URL/products/beveragesqaspx. Escape periods (\.) can indicate that we want to match a text period rather than any old character.

With this rule, when you access/products/beverages. aspx, drinks for sale are displayed on the page. Figure 3 shows a snapshot of the browser accessing/products/beverages. aspx. Please note that in the address bar of the browser, the URL will read/products/beverages. aspx, But what you actually see is listproductsbycategory. aspx? Content of categoryid = 1. (In fact, The/products/beverages. aspx file does not exist on the Web server !)

 

Figure 3. Request category after URL rewriting

Similar to/products/beverages. aspx, we will add rewrite rules for other product categories. This operation only includes adding an additional <rewriterrule> element to the <rules> element of the web. config file. See the web. config file in the downloaded content to obtain a complete set of rewrite rules for this demonstration.

To make the URL more deletable, it is recommended that you delete beverages. aspx from/products/beverages. aspx to see the list of product categories. At first glance, this may be a very common task (you only need to add a rewrite rule that maps/products/to/listcategories. aspx ). However, this operation has a subtle point: you must first create a/products/directory and add an empty default. aspx file to the/products/directory.

To understand the reason for executing these additional steps, refer to the previous content, that is, the URL rewriting engine is located at the ASP. NET level. That is to say, if the ASP. NET engine never has the opportunity to process the request, the URL rewriting engine cannot detect the passed URL. Also, remember that IIS will pass the incoming request to the ASP. net engine only when the requested file has a corresponding extension. Therefore, if the user accesses/products/and IIS does not see any file extension, it will check the directory to see whether such a file exists, that is, the file name is one of the default file names. (Default.aspx1_default.htm, default. asp, and so on. The "documents" tab of the "Web Server properties" dialog box in the "IIS management" dialog box defines these default file names .) Of course, if the/products/directory does not exist, IIS will return an HTTP 404 error.

Therefore, we need to create the/products/directory. In addition, we also need to create a file default. aspx in this directory. In this way, when you access/products/, IIS will check the directory to check whether a file named default. aspx exists, and then pass the processing process to the ASP. NET engine. Then, the URL rewriter will break down when rewriting the URL.

After creating the Directory and the default. aspx file, continue and add the following rewrite rules to the <rules> element:

<Rewriterrule>
<Lookfor> ~ /Products/Default \. aspx </lookfor>
<Sendto> ~ /Listcategories. aspx </sendto>
</Rewriterrule>

With this rule, when users access/products/or/products/default. aspx, they will see the product category list, as shown in 4.

 

Figure 4. Add "deletable" to the URL"

Process sending back
If the URL to be rewritten contains a web form on the server side and the form is resent, The underlined URL is used. That is, if you enter/products/beverages in the browser. aspx. What they will see in the address bar of the browser is/products/beverages. aspx, but the content they see will be listproductsbycategory. aspx? Content of categoryid = 1. If listproductsbycategory. aspx executes sending back, the user will be sent back to listproductsbycategory. aspx? Categoryid = 1 instead of/products/beverages. aspx. This will not interrupt any content, but from the user's perspective, if you suddenly see the URL change when you click the button, it will make them feel uneasy.

The reason for this is that when a web form is displayed, it directly sets its operation attribute to the file path value in the request object. Of course, when rendering a web form, the URL has been rewritten from/products/beverages. aspx to listproductsbycategory. aspx? Categoryid = 1, which indicates that the request object reports that the user wants to access listproductsbycategory. aspx? Categoryid = 1. This problem can be solved simply by making the server-side form not present operation properties. (By default, if the form does not contain operation attributes, the browser will send back .)

Unfortunately, the web form does not allow you to explicitly specify operation properties or set certain properties to disable the rendering of operation properties. Therefore, we must extend the system. Web. htmlcontrols. htmlform class by ourselves to overwrite the renderattribute () method and explicitly state that it will not render the operation attribute.

Because of the inheritance function, we can obtain all the functions of the htmlform class, and only need to add a few linesCodeYou can obtain the desired behavior. The complete code for the custom class is shown below:

Namespace actionlessform {
Public class form: system. Web. UI. htmlcontrols. htmlform
{
Protected override void renderattributes (htmltextwriter writer)
{
Writer. writeattribute ("name", this. Name );
Base. Attributes. Remove ("name ");

Writer. writeattribute ("method", this. method );
Base. Attributes. Remove ("method ");

This. Attributes. Render (writer );

Base. Attributes. Remove ("action ");

If (base. ID! = NULL)
Writer. writeattribute ("ID", base. clientid );
}
}
}

The code of the overwritten renderattributes () method only contains the accurate code of the renderattributes () method of the htmlform class, without setting the operation attribute. (I use Lutz roeder's reflector to viewSource code.)

After this class is created and compiled, it must be applied to ASP. NET web applications.ProgramTo use it, you must first add it to the references folder of the Web application. Then, to use it to replace the htmlform class, you only need to add the following content to the top of the ASP. NET webpage:

<% @ Register tagprefix = "SKM" namespace = "actionlessform"
Assembly = "actionlessform" %>

Then, replace <form runat = "server"> (if any):

<SKM: Form ID = "form1" method = "Post" runat = "server">

And replace the </form> mark on the right:

</SKM: Form>

You can find the custom web form class in the listproductsbycategory. aspx (included in the download code in this article) operation. The downloaded content also contains the Visual Studio. Net project for no web form operation.

Note: If the target URL to be rewritten is not re-sent, you do not need to use this custom web form class.

Back to Top
Create a URL with "deletable"
The simple URL rewriting described in the previous section shows how to easily configure new Rewrite Rules for the URL rewriting engine. However, when using regular expressions, the true function of the rewrite rule will play a greater role. This part will discuss this.

Blog is becoming more and more popular today. It seems that everyone has their own blog. If you are not familiar with blog: a blog is a personal page that is frequently updated and usually used as an online journal. Most blogs only record what happens every day, and some blogs may focus on specific topics (such as movie review, sports team, or computer technology ).

You can update a blog anywhere. The update frequency ranges from the number of times a day to the number of times a week or twice, depending on the author. Generally, the blog homepage displays the last 10 entries, but in fact, all blog software provides archives, allowing visitors to read earlier posts through archives. A blog is a powerful application used to delete a URL. Assume that you find yourself on URL/2004/02/14. aspx when searching for the Blog Archive. Are you surprised to find that you are reading the post in February 14, 2004? In addition, you may want to view all posts in June February 2004. In this case, you can try to delete the URL as/2004/02 /. To view all the posts in June 2004, you can try to access/2004 /.

When maintaining a blog, it is best to provide visitors with a URL of this level that can be deleted ". Many blog engines provide this function, but we will discuss how to use URL rewriting to implement this function.

First, we need an ASP. NET webpage, which displays blog entries by day, month, or year. Suppose we have a showblogcontent. ASPX page, where the query string parameters are year, month, and day. To view the Post published in May February 14, 2004, Can we access showblogcontent. aspx? Year = 2004 & month = 2 & day = 14. To view all posts in February 2004, Can we access showblogcontent. aspx? Year = 2004 & month = 2. Finally, to view all posts in 2004, we can browse showblogcontent. aspx? Year = 2004. (You can find the showblogcontent. aspx code in the download content of this 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.