国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

支持正則表達式的UrlMapping(ASP.NET2.0)

2024-07-10 13:09:49
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  • 猛然發現asp.net 2.0本身就提供了對urlmapping的天然支持--web.config文件中的<urlmappings>節,感嘆現在寫程序真的不是什么技術活了。


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

            </urlmappings>
            <compilation debug="true"/>
      </system.web>
    </configuration>
    這個配置可以使asp.net程序在asp.net development server(就是建asp.net項目時選文件系統)直接支持urlmapping,不過它有幾個不足之處:
    1、只能映射固定的地址,所以只能一個地址一個地址的配置
    2、asp.net development server中可以不用配什么別的地方,在iis中受請求響應模型所限,估計還是要在iis中設映射。這樣的話,反而搞得我到處找資料,看怎么實現在asp.net development server設置映射,得到的結果是不行。

    針對于urlmapping的不支持正則表達式的缺陷,我做了個支持正則表達式的urlmapping,可惜由于urlmapping是由httpapplication調用的,而httpapplication是internal的,不能對它做什么動作,所以實現的東東和urlmapping相比做在web.config中多做個<section>

    文件下載
    (下載文件中包括regexurlmapping組件和一個示例asp.net,注意asp.net程序需部署在iis中,并且要設置映射,方法是右擊虛擬目錄,選屬性,選配置,在通配符應用程序映射中添加c:/windows/microsoft.net/framework/v2.0.50727/aspnet_isapi.dll的引用,并去掉確認文件是否存在的鉤,這里是為了偷懶才用通配符全部映射到asp.net2.0的isapi,實際開發中最好酌情添加具體一點的映射)

    web.config中的配置舉例如下:


    <?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="(?&lt;=/)(?&lt;id&gt;[a-z]+)$" 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>

    其中regexurlmapping的屬性enabled用于打開和關閉映射,rebaseclientpath參見httpcontext.rewritepath中rebaseclientpath參數
    <add>用于添加映射規則,url為匹配路徑的正則表達式pattern,mappedurl是替換規則,用法參見regex.replace方法
    上例中,第一個add在url中用括號定義了組1,所以在后面引用$1
    第二個add在url中用(?<id>)定義了組id,后面用${id}引用了這個組
    第三個是固定字符串替換
    呵呵,看來正則表達式還是很重要滴~~

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 阿拉善左旗| 泰州市| 磴口县| 新源县| 昭觉县| 海盐县| 盐城市| 乐东| 泰宁县| 芷江| 肥城市| 综艺| 蒙阴县| 慈溪市| 建阳市| 沅陵县| 慈溪市| 永城市| 丁青县| 延川县| 鲁甸县| 寻乌县| 常熟市| 邢台市| 余庆县| 郯城县| 临澧县| 大宁县| 长寿区| 泰宁县| 若尔盖县| 陵川县| 乳山市| 东明县| 民勤县| 翁牛特旗| 临泉县| 湾仔区| 富平县| 鸡西市| 石泉县|