UrlMapping (asp.net2.0) that supports regular expressions

Source: Internet
Author: User
Tags config expression file system iis reference regular expression
Asp.net| Regular

Suddenly found that ASP.net 2.0 itself provides a urlmapping of natural support--web.config files in the <urlMappings> section, sigh now write the program is really not a technical life.


<?xml version= "1.0"?>
<configuration>
<system.web>
<urlMappings>
<add url= "~/2006/07" mappedurl= "~/month.aspx?year=2006&month=01"/>
<add url= "~/2006/08" mappedurl= "~/month.aspx?year=2006&month=02"/>

</urlMappings>
<compilation debug= "true"/>
</system.web>
</configuration>
This configuration allows the ASP.net program to support urlmapping directly in the ASP.net Development Server (the file system when the ASP.net project is built), but it has several drawbacks:
1, can only map fixed address, so only one address of the configuration of an address
2, asp.net Development server can not be equipped with any other place, in IIS is limited by the request response model, estimated or to set the mapping in IIS. In this way, instead of making me look around for information, see How to implement the ASP.net Development server settings mapping, the result is not.

In response to UrlMapping's failure to support regular expressions, I made a urlmapping that supports regular expressions, but unfortunately because UrlMapping is invoked by HttpApplication, HttpApplication is internal , can not do anything to it, so the implementation of Dongdong and urlmapping compared to do in web.config more do a <Section>

File download
(The download file includes the Regexurlmapping component and an example asp.net, note that the ASP.net program needs to be deployed in IIS and that you want to set the mapping by right-clicking the virtual directory, selecting Properties, selecting the configuration, adding c:\windows\ in the wildcard application mappings) Microsoft.net\framework\v2.0.50727\aspnet_ Isapi.dll reference, and remove the confirmation file is the existence of the hook, here is to lazy to all map to the asp.net2.0 of the ISAPI, the actual development of the best to add a specific mapping.

Examples of configurations in Web.config are as follows:


<?xml version= "1.0"?>
<configuration>
<configSections>
<section name= "regexurlmappings" type= "Cnblogs.DTC.THIN.RegexUrlMapping.RegexUrlMappingsSection, Cnblogs.DTC.THIN.RegexUrlMapping "/>
</configSections>
<regexurlmappings enabled= "true" Rebaseclientpath= "true" >
<add url= "(\d+) $" mappedurl= "default.aspx?id=$1"/>
<add url= "(? <=/) (? <id>[a-z]+) $" mappedurl= "Default.aspx?id=${id}"/>
<add url= "/$" mappedurl= "/default.aspx?id=0"/>
</RegexUrlMappings>
<system.web>
<add name= "Regexurlmappingmodule" type= "Cnblogs.DTC.THIN.RegexUrlMapping.RegexUrlMappingModule, Cnblogs.DTC.THIN.RegexUrlMapping "/>
<compilation debug= "true"/>
<authentication mode= "Windows"/>
</system.web>
</configuration>

Where regexurlmapping properties are enabled for opening and closing mappings, rebaseclientpath see Rebaseclientpath parameters in Httpcontext.rewritepath
<add> to add a mapping rule, the regular expression Pattern,mappedurl URL is a matching path is a replacement rule, see Regex.Replace method
In the example above, the first add defines group 1 with parentheses in the URL, so the following reference to the
The second add defines the group ID in the URL (?<id>), followed by a reference to the group with ${id}
The third one is fixed string substitution
Oh, it seems that the regular expression is still very important drop ~ ~



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.