Revisit URL Rewrite (2): URL with an existing component Rewrite

Source: Internet
Author: User
Tags config regular expression xml attribute

There may be no one to use the method in the previous article for URL rewrite because the components that provide URL rewrite are already overwhelming.

Asp. Net-level URL rewrite component is simple enough to listen to the BeginRequest event and determine the target URL based on the configuration. In the project I contacted earlier, I found that using urlrewriter as a URL rewrite component is very high, I think it is because that is what Microsoft provides.

If you want to use Urlrewriter, you first naturally configure a HttpModule in web.config:

 

Then it is configured (note: It is highly recommended that you use the Configpath property to extract the configuration into additional files for easy management):

<configSections>
<section name= "rewriterconfig"
type= " URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter "/>
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<lookfor>~/tag/([\w]+)/</ Lookfor>
<sendto>~/tags.aspx? tag=$1</sendto>
</RewriterRule>
</Rules>
</RewriterConfig>

A regular expression is a very important thing that can be matched and captured. In the above example, we put the "/tag/xxx" that meets the lookfor condition Reposition to the tags.aspx page and use XXX as the value of the QueryString item, so that the value can be obtained by httpcontext.request.querystring["tag" in the code.

Urlrewriter's function is sufficient for most applications, but I always dislike it. But if I have to ask why I don't like it, I can't say a Kaniko. Maybe it's just a question of how to configure it. When using URL rewriter, configuration segments tend to be very long and each configuration item requires 4 lines of code from <RewriterRule> to </RewriterRule>, and a small project can easily have hundreds of lines of configuration. "It's too XML," I thought, "Why not use XML attribute?" So that each configuration item can be shortened to 1 lines--but this is a digression.

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.