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:
- <httpModules>
- <add name="ModuleRewriter" type="URLRewriter.ModuleRewriter, URLRewriter" />
- </httpModules>
-
Note: we strongly recommend that you use the configPath attribute to extract the configuration into additional files for ease of 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 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:
- <configSections>
- <section name="rewriter"
- type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler,
- Intelligencia.UrlRewriter" />
- </configSections>
- <rewriter>
- <rewrite url="^/User/(\d+)$" to="~/User.aspx?id=$1" processing="stop" />
- <rewrite url="^/User/(\w+)$" to="~/User.aspx?name=$1" processing="stop" />
- </rewriter>
- <system.web>
- <httpModules>
- <add name="UrlRewriter"
- type="Intelligencia.UrlRewriter.RewriterHttpModule,
- Intelligencia.UrlRewriter" />
- </httpModules>
- </system.web>
- XML and ASP. NET
- Java script in ASP. NET calls the c # Method
- Process of processing ASP. NET Postback Program
- ASP. NET Server-side control CheckBoxList
- Analysis of ASP. NET Membership