乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      NET中關(guān)于偽靜態(tài)的實現(xiàn)方法

       thy 2009-06-23
      讓我們仔細(xì)看一下那些優(yōu)秀網(wǎng)站的網(wǎng)址,我們會發(fā)現(xiàn)他們都是有一定的規(guī)則的,比如按產(chǎn)品分:http://www./prod/1.html,按用戶分:http://www./user/2.html, 這樣的網(wǎng)址看上去比較友好,至少比http://www./user.aspx?id=3這樣要友好的多.那么我們現(xiàn)在就可以思考一下,這是怎么實現(xiàn)的.
        這里要用到的關(guān)鍵技術(shù)是URL重寫技術(shù),咋一聽,比較復(fù)雜,實際上,初次理解,也確實比較復(fù)雜.MS有一篇介紹這個原理的文章,大家可以看一下:http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?mfr=true
        最最簡單的實現(xiàn),就是從上面的這個網(wǎng)址上下載MSDNURLRewriting.msi,安裝完成后,就生成了一個解決方案,里面有實現(xiàn)的原代碼.通常情況下,這些原代碼可以不用改,直接使用.那么我們共同看一下如果實現(xiàn)一個最簡單的重寫.
      1.新建立一個web項目,添加剛才下載下來生成的dll.
      2.修改web.config,這比較重要,所有的重寫規(guī)則都要在這里寫.
        示例:
      在<configuration></configuration>中加入:
           <configSections>
                <section name="RewriterConfig"
      type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
           </configSections>
           <RewriterConfig>
                <Rules>
                    <RewriterRule>
                         <LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
                         <SendTo>~/Default.aspx?ID=$1</SendTo>
                    </RewriterRule>
                </Rules>
           </RewriterConfig>
      然后在<system.web></system.web>中加入:
      <httpHandlers>
         <add verb="*" path="*.aspx"
              type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
      </httpHandlers>
      最后在地址欄上鍵入:http://localhost/Test/2004/12/News.aspx
      效果出來了。
      上面的<LookFor>~/(\d{4})/(\d{2})/News\.aspx</LookFor>這句這正則表達(dá)式URL,即被重寫的URL,而<SendTo>~/Default.aspx?ID=$1</SendTo>這一句為原始URL地址。其中的$1為第一個正則表達(dá)式值(上面例子為:2004),以此類推,第二個即為$2

      簡單吧,這就是最簡單的應(yīng)用了,不過這已經(jīng)可以滿足了最常見的情況了.但是這種方法有一個弊端,那就是不能修改域名前面的部分,什么意思呢??我們經(jīng)常會看到一些網(wǎng)址是類似于這樣的,http://use1.,這可以理解為一個用戶在這個網(wǎng)站上的網(wǎng)址,對于這種方式,上面的方法就不能實現(xiàn)了,這要用到兩種技術(shù),泛域名解析+URL重寫.
      什么是泛域名解析,這個網(wǎng)上的解釋很多,我在這里就不多說了.在做完泛域名解析后,就可以修改web.config里面的內(nèi)容了,
      只需改成下面的樣子就可以了.
      <configuration>
         
        <configSections>
        <section name="RewriterConfig"
      type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
        </configSections>
       
        <RewriterConfig>
        <Rules>
       
        <RewriterRule>
        <LookFor>~/([a-zA-Z0-9]*)\.aspx</LookFor>
        <SendTo>~/List.aspx?Info=$1</SendTo>
        </RewriterRule>
       
        <RewriterRule>
        <LookFor>~/([a-zA-Z0-9]*)\.html</LookFor>
        <SendTo>~/List.aspx?Info=$1</SendTo>
        </RewriterRule>
       
        <RewriterRule>
        <LookFor>http://([a-zA-Z0-9]*)\.blog\.chp365\.cn/</LookFor>
        <SendTo>~/webuserblog/$1/default.htm</SendTo>
        </RewriterRule>
       
        </Rules>
        </RewriterConfig>
        <system.web>
        <httpModules>
        <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
        </httpModules>
      另外,還要在IIS中,添加一個通配符應(yīng)用程序映射,指到aspx的那個文件就可以了.
      下面我引用一下網(wǎng)友KILLHAND的兩篇文章來說一下如何在實踐中運用.他的文章寫的很好.
      UrlReWriter 使用經(jīng)驗小結(jié)
      #UrlRewriter 是微軟封裝好了的一個URL重寫組件。使用它可以讓我節(jié)約很多自已開發(fā)的時間。
      好了,開始講述我的應(yīng)用經(jīng)驗,這只是很菜鳥的經(jīng)驗,高手就不用看了。
      第一步,請從此下載此組件。解壓,把UrlRewriter.dll copy到你的項目 bin 目錄下。
      第二步,在Web.config中加入:
      <?xml version="1.0" encoding="gb2312" ?>
      <configuration>
           <configSections>
                <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
           </configSections>
      第二步,加入重寫的規(guī)則節(jié)點:
      如: 
         <RewriterConfig>
                <Rules>
                    <RewriterRule>
                         <LookFor>~/Sell/(.[0-9]*)\.html</LookFor>
                         <SendTo>~/Search/Search_Sell.aspx?id=$1</SendTo>
                    </RewriterRule>
                    <RewriterRule>
                         <LookFor>~/Sell/Search_Sell\.aspx</LookFor>
                         <SendTo>~/Search/Search_Sell.aspx</SendTo>
                    </RewriterRule>
                    <RewriterRule>
             <LookFor>~/Buy/(.[0-9]*)\.html</LookFor>
                         <SendTo>~/Search/Search_Buy.aspx?id=$1</SendTo>
                    </RewriterRule>
                    <RewriterRule>
             <LookFor>~/Buys/(.[0-9]*)\.html</LookFor>
                         <SendTo>~/Buys/Show.aspx?id=$1</SendTo>
                    </RewriterRule>
                </Rules>
           </RewriterConfig>
      這個就要根據(jù)你的需要了,如果你對正則表達(dá)式不熟,那么沒辦法,要么憑借你的高智商去找其中規(guī)律,稍稍改一下就能為你所用了。呵呵。如果實在搞不清,那就自己GOOGLE一下正則表達(dá)式吧。(本人開始是參考別人的配置猜的,竟然用對了,呵呵。后來還是看了一下相關(guān)資料,發(fā)現(xiàn)這東東很有用。)
      第三步,加入模塊配置(寫在<system.web>里面):
      如:
       <httpHandlers>
           <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
        </httpHandlers>
      (這里表示使用HTTP程序來處理重寫)
      好了,到了現(xiàn)在我們可以試一下看。
      于是輸入:http://127.0.0.1:8080/Sell/1.aspx 出現(xiàn)了,呵呵。但是如果所它改為:http://127.0.0.1:8080/Sell/1.html
      暈,發(fā)現(xiàn)不行。汗。。。
      呵呵,原因是沒把HTML的解析用 asp.net  的ISAPI來解析。
      辦法是。。。
      第四步,在IIS\你的站點\屬性\主目錄\配置\映謝 加入一個和 aspx 頁面的配置相同的擴(kuò)展名項。注意“確認(rèn)文件是否存在”不要勾選,否則會出現(xiàn)找不到文件。
      現(xiàn)在再來試試看。什么?#¥%#¥%#,還是不行。呵呵。不要急,咱們回過頭再來看看,原來在 web.config 中我們沒有配置 .html 也使用模塊此解析。
      第五步,在模塊配置中加入:
        <httpHandlers>
           <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
           <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
        </httpHandlers>
      現(xiàn)在總可以了吧,呵呵。終于看到了,興奮吧。不要急,這還只是最簡單的。如果你的頁面有回傳。比如說放了DATAGRID,有分頁的,你點到下一頁就發(fā)現(xiàn),暈倒,又出問題了。
      這下怎么辦呢,這個其實微軟件的網(wǎng)站上就有說到,我在這里簡述一下了。

      第六步,加入窗體回傳保持的組件:
      在原來你下載的項目里找到 ActionlessForm.dll 放到你的項目 bin 目錄下。
      然后在你的這個頁面中加入:
      <%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
      再把你的<Form...>改為:
      <skm:Form id="你的表單名" method="post" runat="server">
      .....
      </skm:Form>
      That's All.現(xiàn)在你可以高枕無憂了。一切如你所愿。
      最后,恭祝各位一切順利。
      利用UrlRewriter 實現(xiàn)二級域名
      從上一篇文章,我們可以實現(xiàn)對域名后面的那部分進(jìn)行重寫,那么可不可以對前面那部分進(jìn)行重寫而實現(xiàn)二級域名呢?
      答案是肯定的。
      這樣,首先我們得修改UrlRewriter,怎么修改請參見江大魚的BLog。
      1.BaseModuleRewriter.cs
       
      protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
              {
                  HttpApplication app = (HttpApplication) sender;
                  Rewrite(app.Request.Path, app);
              }
      改為
       
      protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
              {
                  HttpApplication app = (HttpApplication) sender;
                  Rewrite(app.Request.Url.AbsoluteUri, app);
              }

      就是將  app.Request.Path 替換成了  app.Request.Url.AbsoluteUri
      2.ModuleRewriter.cs
       
      for(int i = 0; i < rules.Count; i++)
                  {
                      // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                      string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
       
                      // Create a regex (note that IgnoreCase is set)
                      Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
       
                      // See if a match is found
                      if (re.IsMatch(requestedPath))
                      {
                          // match found - do any replacement needed
                          string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
       
                          // log rewriting information to the Trace object
                          app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
       
                          // Rewrite the URL
                          RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                          break;        // exit the for loop
                      }
                  }
      改為
       
      for(int i = 0; i < rules.Count; i++)
                  {
                      // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                      string lookFor = "^" + rules[i].LookFor + "$";
       
                      // Create a regex (note that IgnoreCase is set)
                      Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
       
                      // See if a match is found
                      if (re.IsMatch(requestedPath))
                      {
                          // match found - do any replacement needed
                          string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
       
                          // log rewriting information to the Trace object
                          app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
       
                          // Rewrite the URL
                          RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                          break;        // exit the for loop
                      }
                  }
      string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
      改成了
      string lookFor = "^" + rules[i].LookFor + "$";

      完成這2處改動之后重新編譯項目,將生成的dll復(fù)制到bin目錄下。
      修改完了這后,我們再把此 UrlRewriter.dll COPY 到我們項目的Bin目錄下。這樣就結(jié)了么?沒有。
      首先請確定你的項目之前有按我上篇文章中寫到的那樣做過UrlRewriter的配置,否則請先回過頭來看看那篇文章。
      如果你的項目已配置過,那么,我們還要為此做以下幾件事情:
      1。請確定你的域名是支持泛解析的。然后你的網(wǎng)站為默認(rèn)網(wǎng)站,否則將不能實現(xiàn)(至少我現(xiàn)在還沒有找到好辦法)
      2。在IIS配置:在IIS\你的站點\屬性\主目錄\配置\映謝 在通配符應(yīng)用程序配置處插入一個新的映謝。把可執(zhí)行文件設(shè)為和上面ASPX頁面同樣的配置即可(注意不要勾選 “確定文件是否存在”)。(用處就是使所有請求通過 asp.net 的ISAPI來處理,只有這樣才能對所有地址進(jìn)行重寫嘛。)
      3。查看下你的網(wǎng)站主機(jī)頭,里面的第一個主機(jī)頭值必須為空,否則會出現(xiàn)錯誤的請求。后面就隨你加了,看你想綁定多少域名了。(這個辦法是江大魚想出來的。為這個錯誤我們都想了好多辦法。在這里感謝江大魚。。。)
      4。最后改寫你的 web.config 文件。
      把上節(jié)中說到的
        <httpHandlers>
           <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
           <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
        </httpHandlers>
      改為:
        <httpModules>
         <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
        </httpModules>
      (就是改用HTTP 模塊來執(zhí)行重寫,而不用HTTP 程序,否則無法重寫地址前面。)
      然后就來修改我們的重寫正則了:
                    <RewriterRule>
                         <LookFor>http://(.[0-9]*)\.178b2b\.com/</LookFor>
                         <SendTo>~/Search/Search_Sell.aspx?id=$1</SendTo>
                    </RewriterRule>
      好了,現(xiàn)在你輸入 http://1./ 就能搜索出相應(yīng)分類了。但是聰明的你馬上就發(fā)現(xiàn)。暈死,首頁進(jìn)不去了。呵呵。當(dāng)然嘍。你還得為首頁加入重寫正則。
                    <RewriterRule>
                         <LookFor>http://www\.178b2b\.com/</LookFor>
                         <SendTo>~/index.htm</SendTo>
                    </RewriterRule>
      大功告成。感覺爽死了吧。呵呵。莫急,如果你二級域名指向的目錄下面的頁面都用的相對地址連接的圖片和其它頁面的話,呵呵,你有得忙了,你要全部改成如下方式:
      <a href=http://www./cxlm/league.html target="_blank">誠信聯(lián)盟</a>
      以上就是用UrlRewriter實現(xiàn)二級域名的方法了。希望各位一切順利。
      最后,感謝江大魚的無私幫助。

      再次對KILLHEAD和江大魚表示感謝.
      說的不是很清楚,那是因為我文筆不好,如果有朋友想共同研究一下的話,可以加我QQ:120854833.或email給我:chenghp986@sohu.com
       

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多