net 实现 URL重写,伪静态

net 实现 URL重写,伪静态

一,获得Mircosoft URLRewriter.dll:
获得Mircosoft URLRewriter.dll可以到http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?mfr=true


下载完毕后,导入工程,我这里没有对该工程做任何修改,保留了原来的重写方式,然后直接在VS2005里面生成.dll文件就可以了。
二,使用该dll文件:
添加引用,搞定。
三,页面方面的设计,这里不在赘述了,我会放一个下载包,有兴趣的朋友下载来看看吧,代码写的比较乱。
四,web.config的配置
这部是非常关键的,也是静态化能否成功的关键。
Java代码 复制代码
  1. <?xml version="1.0"?>    
  2. <configuration>    
  3. <configSections>    
  4.     <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />    
  5. </configSections>    
  6.   
  7. <RewriterConfig>    
  8.         <Rules>    
  9.             <RewriterRule>    
  10.                 <LookFor>~/web/new/type/(.[0-9]*)/.html</LookFor>    
  11.         <SendTo>~/web/new.aspx?id=$1</SendTo>    
  12.             </RewriterRule>    
  13.       <RewriterRule>    
  14.         <LookFor>~/web/index.html</LookFor>    
  15.         <SendTo>~/web/index.aspx</SendTo>    
  16.       </RewriterRule>    
  17.         </Rules>    
  18.     </RewriterConfig>    
  19.     <system.web>    
  20.     <httpHandlers>    
  21.       <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />    
  22.       <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />    
  23.     </httpHandlers>    
  24.         <compilation debug="true"/></system.web>    
  25. </configuration>    
  26. <?xml version="1.0"?>   
  27. <configuration>   
  28. <configSections>   
  29.     <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />   
  30. </configSections>   
  31.   
  32. <RewriterConfig>   
  33.         <Rules>   
  34.             <RewriterRule>   
  35.                 <LookFor>~/web/new/type/(.[0-9]*)/.html</LookFor>   
  36.         <SendTo>~/web/new.aspx?id=$1</SendTo>   
  37.             </RewriterRule>   
  38.       <RewriterRule>   
  39.         <LookFor>~/web/index.html</LookFor>   
  40.         <SendTo>~/web/index.aspx</SendTo>   
  41.       </RewriterRule>   
  42.         </Rules>   
  43.     </RewriterConfig>   
  44.     <system.web>   
  45.     <httpHandlers>   
  46.       <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />   
  47.       <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />   
  48.     </httpHandlers>   
  49.         <compilation debug="true"/></system.web>   
  50. </configuration>  
<?xml version="1.0"?> 
<configuration> 
<configSections> 
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> 
</configSections> 

<RewriterConfig> 
        <Rules> 
            <RewriterRule> 
                <LookFor>~/web/new/type/(.[0-9]*)/.html</LookFor> 
        <SendTo>~/web/new.aspx?id=$1</SendTo> 
            </RewriterRule> 
      <RewriterRule> 
        <LookFor>~/web/index.html</LookFor> 
        <SendTo>~/web/index.aspx</SendTo> 
      </RewriterRule> 
        </Rules> 
    </RewriterConfig> 
    <system.web> 
    <httpHandlers> 
      <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> 
      <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> 
    </httpHandlers> 
        <compilation debug="true"/></system.web> 
</configuration> 
<?xml version="1.0"?>
<configuration>
<configSections>
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>

<RewriterConfig>
        <Rules>
            <RewriterRule>
                <LookFor>~/web/new/type/(.[0-9]*)/.html</LookFor>
        <SendTo>~/web/new.aspx?id=$1</SendTo>
            </RewriterRule>
      <RewriterRule>
        <LookFor>~/web/index.html</LookFor>
        <SendTo>~/web/index.aspx</SendTo>
      </RewriterRule>
        </Rules>
    </RewriterConfig>
    <system.web>
    <httpHandlers>
      <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
      <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
    </httpHandlers>
        <compilation debug="true"/></system.web>
</configuration>



这里简单介绍一下
Java代码 复制代码
  1. <RewriterConfig>    
  2.    <Rules>    
  3.    <RewriterRule>    
  4.       <LookFor>要查找的模式</LookFor>    
  5.       <SendTo>要用来替换模式的字符串</SendTo>    
  6.    </RewriterRule>    
  7.    <RewriterRule>    
  8.       <LookFor>要查找的模式</LookFor>    
  9.       <SendTo>要用来替换模式的字符串</SendTo>    
  10.    </RewriterRule>    
  11.    </Rules>    
  12. </RewriterConfig>    
  13. <RewriterConfig>   
  14.    <Rules>   
  15.    <RewriterRule>   
  16.       <LookFor>要查找的模式</LookFor>   
  17.       <SendTo>要用来替换模式的字符串</SendTo>   
  18.    </RewriterRule>   
  19.    <RewriterRule>   
  20.       <LookFor>要查找的模式</LookFor>   
  21.       <SendTo>要用来替换模式的字符串</SendTo>   
  22.    </RewriterRule>   
  23.    </Rules>   
  24. </RewriterConfig>  
<RewriterConfig> 
   <Rules> 
   <RewriterRule> 
      <LookFor>要查找的模式</LookFor> 
      <SendTo>要用来替换模式的字符串</SendTo> 
   </RewriterRule> 
   <RewriterRule> 
      <LookFor>要查找的模式</LookFor> 
      <SendTo>要用来替换模式的字符串</SendTo> 
   </RewriterRule> 
   </Rules> 
</RewriterConfig> 
<RewriterConfig>
   <Rules>
   <RewriterRule>
      <LookFor>要查找的模式</LookFor>
      <SendTo>要用来替换模式的字符串</SendTo>
   </RewriterRule>
   <RewriterRule>
      <LookFor>要查找的模式</LookFor>
      <SendTo>要用来替换模式的字符串</SendTo>
   </RewriterRule>
   </Rules>
</RewriterConfig>



httpHandlers的设置主要是配合IIS将请求重新定义处理,这里也比较关键,如果不存在合理的httpHandlers,那么,访问肯定会失败的。

关于正则表达式,可以到百度里搜索:"常用正则表达式",会有很多。

五.配置IIS解析.html文件
右键点我的电脑-->管理-->展开'服务和应用程序'-->internet信息服务-->找到你共享的目录-->右键点击属性 -->点击'配置'-->映射下面 -->找到.aspx的可执行文件路径 复制路径-->粘贴路径-->扩展名为".html"-->然后把检查文件是否存在的勾去掉这样就可以了,如果遇到“确定”按钮失效,可以用键盘事件编辑路径即可解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值