现在的位置: 首页 > 综合 > 正文

asp.net iis7 伪静态实现

2017年08月01日 ⁄ 综合 ⁄ 共 1841字 ⁄ 字号 评论关闭

看了N多文章找到几个关键点

1.iis7以上用不上isapi组件

2.使用 URLRewriter  来做处理

3.web.config配置要点

 添加声明

<configuration>
  <configSections>
    <!--伪静态用-->
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
    <!--结束-->
  </configSections>

添加处理

<!--伪静态用-->
    <httpHandlers>
      <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
      <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
    </httpHandlers>
    <httpModules>
      <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
    </httpModules>
  </system.web>

最后是关键一步一定要加否则无法跳转

</system.web>
  <system.webServer>
    <handlers>
      <add name=".html" path="*.html" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
    </handlers>
  </system.webServer>

下面就是配置规则了

<RewriterConfig>
    <Rules>

     <!--特殊的-->
      <RewriterRule>
        <LookFor>~/Download-(.[\d]*)\.html</LookFor>
        <SendTo>~/Download.aspx?c=$1</SendTo>
      </RewriterRule>

      <RewriterRule>
        <LookFor>~/UserOrderDetail-(.[\d]*)\.html</LookFor>
        <SendTo>~/UserOrderDetail.aspx?code=$1</SendTo>
      </RewriterRule>

      <RewriterRule>
        <LookFor>~/News-(.[\d]*)-(.[\d]*)\.html</LookFor>
        <SendTo><![CDATA[~/News.aspx?c=$1&id=2]]></SendTo>
      </RewriterRule>

      <RewriterRule>
        <LookFor>~/Message_(.[\d]*)-(.[\d]*)\.html</LookFor>
        <SendTo><![CDATA[~/Message.aspx?page=$1]]></SendTo>
      </RewriterRule>

      <!--下面是通用的-->
      <RewriterRule>
        <LookFor>~/(\w*)\.html</LookFor>
        <SendTo>~/$1.aspx</SendTo>
      </RewriterRule>
      <RewriterRule>
        <LookFor>~/(\w*)-(.[\d]*)\.html</LookFor>
        <SendTo>~/$1.aspx?id=$2</SendTo>
      </RewriterRule>


      <RewriterRule>
        <LookFor>~/(\w*)-(\w*)_(.[\d]*)\.html</LookFor>
        <SendTo><![CDATA[~/$1.aspx?c=$2&page=$3]]></SendTo>
      </RewriterRule>

    <!--建议:跳转的搞成按参数做不同的规则的话配置几种就Ok了,而不用像我这个还搞几个特殊的东东出来-->

    </Rules>
  </RewriterConfig>

抱歉!评论已关闭.