[筆記] 技巧/訣竅:在ASP.NET中重寫URL

來源:互聯網
上載者:User

原文地址:
技巧/訣竅:在ASP.NET中重寫URL http://blog.joycode.com/scottgu/archive/2007/03/01/94004.aspx

方法一:使用Request.PathInfo 參數而不是查詢字串
方法二:使用HttpModule實現URL重寫
方法三:在IIS7中使用HttpModule 實現無副檔名的URL重寫
方法四:在IIS5和IIS6中使用 ISAPIRewrite 來實現無副檔名的URL重寫 
在URL重寫裡處理ASP.NET PostBack
正確地處理CSS和映像引用

常式的URL重寫情境
http://www.store.com/products.aspx?category=books

http://www.store.com/products.aspx?category=DVDs

http://www.store.com/products.aspx?category=CDs


方法一:使用Request.PathInfo 參數而不是查詢字串

http://www.store.com/products.aspx/Books

http://www.store.com/products.aspx/DVDs

http://www.store.com/products.aspx/CDs

 

方法二:使用HttpModule實現URL重寫

http://www.store.com/products/Books.aspx

http://www.store.com/products/DVDs.aspx

http://www.store.com/products/CDs.aspx

 

方法三:在IIS7中使用HttpModule 實現無副檔名的URL重寫

http://www.store.com/products/Books

http://www.store.com/products/DVDs

http://www.store.com/products/CDs       

      IIS 7.0使得處理這類情形容易之極。你現在可以在 IIS 請求管道的任何地方執行一個HttpModule,這意味著你可以使用上面的URLRewriter 模組來處理和重寫無副檔名的URL(甚至是帶有 .asp,.php,或 .jsp 副檔名的URL)。下面示範了你在IIS7中該如何配置:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

  <configSections>
    <section name="rewriter" 
             requirePermission="false" 
             type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
  </configSections>
  
  <system.web>      
    <httpModules>
      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
    </httpModules>    
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" />
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>

  <rewriter>
    <rewrite url="~/products/(.+)" to="~/products.aspx?category=$1" />
  </rewriter>
  
</configuration> 



方法四:在IIS5和IIS6中使用 ISAPIRewrite 來實現無副檔名的URL重寫



在URL重寫裡處理ASP.NET PostBack

      當使用URL重寫時,會出現這樣的問題,<form> 控制項顯示的URL不是原先請求的URL(譬如,/products/books),而是重寫過後的URL(譬如,/products.aspx?category=books)。這意味著,當你做一個postback到伺服器時,URL不再是你原先乾淨利落的那個了。       在ASP.NET 2.0中,有個比較乾淨的訣竅你可以用來重寫<form>控制項的action屬性。具體地來說,你可利用新的ASP.NET 2.0控制項適配器擴充架構來定製控制項的輸出,用你提供的值來覆蓋action屬性的值。這不要求在你的.aspx頁面裡做任何編碼改動,而只要在你的/app_browsers檔案夾裡添加一個.browser檔案,註冊使用一個控制項適配類即可輸出新的action屬性。


 
正確地處理CSS和映像引用

      不少人在第一次使用URL重寫時,有時會遇上一個疑難雜症,就是他們發現他們的映像和CSS樣式表引用有時會停止工作。這是因為他們在HTML網頁裡有對這些檔案的相對參照,當你開始在應用裡重寫URL時,你需要意識到瀏覽器經常會在不同的邏輯階層層上(logical hierarchy levels)請求檔案,而不是實際儲存在伺服器上的東西。

      譬如,如果我們上面的/products.aspx網頁對.aspx 網頁裡的logo.jpg有一個相對參照,但是通過 /products/books.aspx這個URL來請求的,那麼瀏覽器在顯示網頁時,將會發出一個對/products/logo.jpg的請求,而不是對/logo.jpg的請求。要正確地引用這個檔案,確認你用根目錄限定了(root qualify)CSS和映像引用(“/style.css”,而不是 “style.css”)。對於ASP.NET控制項,你也可以使用“~”句法從你應用的根目錄來引用檔案(譬如,<asp:image imageurl="~/images/logo.jpg" runat="server"/>) 。

  附:/Files/kiant/2010.05/UrlRewrite_HttpModule1.FormRewriter.cs.zip  附加CS版的檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.