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

首頁(yè) > 編程 > .NET > 正文

asp.net URL重寫(xiě)(URLRewriter) 簡(jiǎn)化版

2024-07-10 13:12:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  在 asp.net 里實(shí)現(xiàn) url重寫(xiě)(urlrewriter)的一個(gè)最簡(jiǎn)單的方法。

  參考了 (作者 scott mitchell 翻譯:janssen )的大作,雖然沒(méi)有完全看明白,但是也照貓畫(huà)虎地做了一個(gè),頗有“成就”感。寫(xiě)出來(lái)分享一下。

  原作里講了很多的原理,這里就不說(shuō)了(其實(shí)我也不懂)。這里就寫(xiě)操作過(guò)程吧。目的是實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的能實(shí)現(xiàn) url重寫(xiě) 的程序。

  1、需要設(shè)置一下iis里的站點(diǎn)屬性。

  2、修改web.config的內(nèi)容。

   <system.web>
   <httphandlers>
   <add verb="*" path="*.zhtml" type="zdil.urlrewriter.rewriterfactoryhandler, zdilurlrewriter" />
  </httphandlers>
  </system.web>

  其中*.zhtml 就是地址欄里面寫(xiě)的網(wǎng)頁(yè)的擴(kuò)展名,就是給用戶看的,這個(gè)可以隨意改(但是要符合擴(kuò)展名的規(guī)則?。.?dāng)然要和第一步里面的設(shè)置相一致才行。

  3、寫(xiě)一個(gè)類。

  using system;
  using system.io;
  using system.web;
  using system.web.ui;

  namespace zdil.urlrewriter
  {
   /**//// <summary>
   /// url重寫(xiě)
  /// </summary>
   public class rewriterfactoryhandler : ihttphandlerfactory
    {
    /**//// <summary>
   /// gethandler is executed by the asp.net pipeline after the associated httpmodules have run.  the job of
        /// gethandler is to return an instance of an httphandler that can process the page.
        /// </summary>
        /// <param name="context">the httpcontext for this request.</param>
        /// <param name="requesttype">the http data transfer method (<b>get</b> or <b>post</b>)</param>
        /// <param name="url">the rawurl of the requested resource.</param>
        /// <param name="pathtranslated">the physical path to the requested resource.</param>
        /// <returns>an instance that implements ihttphandler; specifically, an httphandler instance returned
        /// by the <b>pageparser</b> class, which is the same class that the default asp.net pagehandlerfactory delegates
        /// to.</returns>
        public virtual ihttphandler gethandler(httpcontext context, string requesttype, string url, string pathtranslated)
        {
            string sendtourl = url;                         //地址欄里面的地址
            string filepath = pathtranslated;

            string sendtourlstring = "/web/index.aspx";  //真正要訪問(wèn)的頁(yè)面
            string querystring = "";                     //參數(shù)。比如 ?id=123

            filepath = context.server.mappath(sendtourlstring); //物理地址

            //這句最重要了。轉(zhuǎn)向了。
            context.rewritepath(sendtourlstring, string.empty, querystring);
           
            //這個(gè)還沒(méi)有弄明白 :)
            return pageparser.getcompiledpageinstance(url, filepath, context);
        }

        public virtual void releasehandler(ihttphandler handler)
        { //這個(gè)也不懂了
        }
    }
}
 

  這個(gè)類呢,要寫(xiě)在一個(gè)單獨(dú)的項(xiàng)目里面,然后編譯成 zdilurlrewriter.dll文件。(注意文件名,寫(xiě)錯(cuò)了就不能正常運(yùn)行了)。

  4、完成了。

  打開(kāi)ie ,在地址欄里輸入 http://.../1.zhtml。

  瀏覽者看到是一個(gè)靜態(tài)頁(yè)的地址,但是實(shí)際上訪問(wèn)的卻是 /web/index.aspx 這個(gè)動(dòng)態(tài)網(wǎng)頁(yè)。

  怎么樣簡(jiǎn)單吧。

  當(dāng)然了,這個(gè)是最簡(jiǎn)單的,簡(jiǎn)單到了“不能用”的地步了。因?yàn)樗麜?huì)把所有的 *.zhtml 的訪問(wèn)都“重寫(xiě)”到 /web/index.aspx 。

  至于把什么樣的網(wǎng)頁(yè)重寫(xiě)到哪個(gè)網(wǎng)頁(yè),這里就不介紹了(這里只講方法,不講實(shí)現(xiàn)的細(xì)節(jié))。

  方法很多了,原作是通過(guò)正則來(lái)匹配的,我是通過(guò) string sendtourl = url; 來(lái)判斷的。

  其他的就看你們的需要了。

  http://blog.csdn.net/shixin1198/archive/2006/10/16/1336846.aspx

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 怀仁县| 中超| 类乌齐县| 宁陕县| 垫江县| 兰州市| 五寨县| 南陵县| 延边| 黄平县| 建平县| 昆明市| 镇沅| 施甸县| 汽车| 嘉荫县| 江北区| 宜宾市| 隆昌县| 宜都市| 保亭| 浦东新区| 乐昌市| 内江市| 佳木斯市| 杨浦区| 石柱| 三门峡市| 石首市| 甘德县| 大丰市| 涿州市| 松滋市| 唐海县| 腾冲县| 托里县| 德庆县| 孙吴县| 景宁| 乌苏市| 盐城市|