URL Rewrite component of ASP. NET

Source: Internet
Author: User
Tags xml attribute

No one may use the method in the previous ASP. NET article to perform URL Rewrite, because the URL Rewrite component is already provided.

The principle of the ASP. NET-level URL Rewrite component is very simple. In fact, it only listens to the BeginRequest event and determines the target URL according to the configuration. In my previous projects, I found that URLRewriter is frequently used as the URL Rewrite component. I think it may be because it is something provided by Microsoft.

To use URLRewriter, configure an HttpModule in web. config:

 
 
  1. <httpModules> 
  2. <add name="ModuleRewriter" type="URLRewriter.ModuleRewriter, URLRewriter" /> 
  3. </httpModules> 
  4.  


Note: we strongly recommend that you use the configPath attribute to extract the configuration into additional files for ease of management ):

 
 
  1. <configSections> 
  2. <section name="RewriterConfig" 
  3. type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> 
  4. </configSections> 
  5. <RewriterConfig> 
  6. <Rules> 
  7. <RewriterRule> 
  8. <LookFor>~/tag/([\w]+)/</LookFor> 
  9. <SendTo>~/Tags.aspx?Tag=$1</SendTo> 
  10. </RewriterRule> 
  11. </Rules> 
  12. </RewriterConfig> 

A regular expression is a very bad thing. It can be matched and captured. In the preceding example, we locate/tag/xxx that meets the LookFor condition to Tags. on the aspx page, xxx is used as the value of the Tag QueryString item, so that you can use HttpContext in the code. request. queryString ["Tag"] to obtain this value.

The URL Rewriter feature is sufficient for most applications, but I always don't like it. But if I have to ask why I don't like it, it's hard for me to say it's ugly. This configuration method may be the only problem. When URL Rewriter is used, the configuration segment is often very long. Each configuration item requires four lines of code from <RewriterRule> to </RewriterRule>, for a small project, hundreds of rows of configuration are easily displayed. "This is too XML." I think, why not use XML Attribute? In this way, each configuration item can be shortened to one line -- however, this is an issue of ASP. NET.

Therefore, if I want to do URL Rewrite, I usually use the open source component Url Rewriter. NET produced by Intelligencia. Although this name is very similar to the previous one, it has far more functions than the former. The usage of this component is close to that of URL Rewriter. In fact, it seems that all URL Rewrite components are similar). All we need to do is configure:

 
 
  1. <configSections> 
  2. <section name="rewriter" 
  3. type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler,  
  4. Intelligencia.UrlRewriter" /> 
  5. </configSections> 
  6. <rewriter> 
  7. <rewrite url="^/User/(\d+)$" to="~/User.aspx?id=$1" processing="stop" /> 
  8. <rewrite url="^/User/(\w+)$" to="~/User.aspx?name=$1" processing="stop" /> 
  9. </rewriter> 
  10. <system.web> 
  11. <httpModules> 
  12. <add name="UrlRewriter" 
  13. type="Intelligencia.UrlRewriter.RewriterHttpModule,  
  14. Intelligencia.UrlRewriter" /> 
  15. </httpModules> 
  16. </system.web> 
  1. XML and ASP. NET
  2. Java script in ASP. NET calls the c # Method
  3. Process of processing ASP. NET Postback Program
  4. ASP. NET Server-side control CheckBoxList
  5. Analysis of ASP. NET Membership

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.