且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-13 09:04:38


 

 下载完毕后,导入工程,我这里没有对该工程做任何修改,保留了原来的重写方式,然后直接在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,如需转载请自行联系原作者