.net 实现 URL重写,伪静态(转)

本文介绍如何通过自定义DLL实现ASP.NET项目的URL静态化,包括DLL的创建、web.config中关键配置项的设置及IIS对.html文件的解析配置。

 

 下载完毕后,导入工程,我这里没有对该工程做任何修改,保留了原来的重写方式,然后直接在VS2005里面生成.dll文件就可以了。
二,使用该dll文件:
添加引用,搞定。
三,页面方面的设计,这里不在赘述了,我会放一个下载包,有兴趣的朋友下载来看看吧,代码写的比较乱。
四,web.config的配置
这部是非常关键的,也是静态化能否成功的关键。
view plaincopy to clipboardprint?
<?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>
 

这里简单介绍一下:


 

view plaincopy to clipboardprint?
<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"-->然后把检查文件是否存在的勾去掉这样就可以了,如果遇到“确定”按钮失效,可以用键盘事件编辑路径即可解决。




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/articles/1499341.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值