随笔-7  评论-24  文章-102  trackbacks-0

原文地址:
技巧/诀窍:在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版的文件
posted on 2010-05-11 10:52 黄小二 阅读(329) 评论(0)  编辑  收藏 所属分类: ASP.NET

只有注册用户登录后才能发表评论。


网站导航: