Suddenly I discovered that ASP.NET 2.0 itself provides natural support for UrlMapping - the <urlMappings> section in the web.config file. I lamented that writing programs is really not a technical job nowadays.
<?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 directly support UrlMapping in the ASP.NET Development Server (that is, select the file system when building an ASP.NET project), but it has several shortcomings:
1. Only fixed addresses can be mapped, so only one address can be configured.
2. There is no need to configure anything else in ASP.NET Development Server. Due to the request response model in IIS, it is estimated that mapping needs to be set up in IIS. In this case, I was looking for information everywhere to see how to set up mapping in ASP.NET Development Server, but the result was that it didn't work.
In view of the shortcoming of UrlMapping that it does not support regular expressions, I made an UrlMapping that supports regular expressions. Unfortunately, because UrlMapping is called by HttpApplication, and HttpApplication is Internal, it cannot do anything with it, so the implementation is unreliable. Compared with UrlMapping,
download an additional <Section> file in Web.config (the downloaded file includes the RegexUrlMapping component and a sample ASP.NET. Note that the ASP.NET program needs to be deployed in IIS and the mapping must be set. The method is Right-click the virtual directory, select Properties, select Configuration, add a reference to c:windowsmicrosoft.netframeworkv2.0.50727aspnet_isapi.dll in the wildcard application mapping, and remove the hook to confirm whether the file exists, here is To be lazy, I use wildcards to map everything to the ISAPI of ASP.NET 2.0. In actual development, it is best to add more specific mappings as appropriate)
The configuration examples 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>[az]+)$" mappedUrl="default.aspx?id=${id}" />
<add url="/$" mappedUrl="/default.aspx?id=0"/>
</RegexUrlMappings>
<system.web>
<httpModules>
<add name="RegexUrlMappingModule" type="Cnblogs.DTC.THIN.RegexUrlMapping.RegexUrlMappingModule,Cnblogs.DTC.THIN.RegexUrlMapping"/>
</httpModules>
<compilation debug="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>
The enabled attribute of RegexUrlMapping is used to open and close mapping. For rebaseClientPath, please refer to the rebaseClientPath parameter in HttpContext.RewritePath.
<add> is used to add mapping rules, url is the regular expression pattern that matches the path, and mappedUrl is the replacement rule. For usage, see the Regex.Replace method. In the above example, the first add defines group 1 with parentheses in the url, so in Reference later $1
The second add uses (?<id>) in the URL to define the group id, and then uses ${id} to reference the group. The third is fixed string replacement. It seems that regular expressions are still very important~~
http://www.cnblogs.com/thinhunan/archive/2006/08/22/regexurlmapping.html