zoukankan      html  css  js  c++  java
  • Asp.Net实现伪静态(通过URLRewriter)

    一、起因

    最近一个项目要实现伪静态,之前没接触过,故一切从零开始,开始网上查资料,方法大概有三种,但是我都试了好几个,都失败了。望有建议的博友给点建议,这里我实现了一种方式,是通过微软URLRewriter.dll实现的,觉得不错,故记录下来,大家一起学习,话不多说,上代码。

    效果截图:

    二、配置web.config

    首先,新建项目。添加一个页面Default.aspx,用于测试。去下一个URLRewriter.dll,链接: https://pan.baidu.com/s/1hs3YNZm 密码: n6jx,添加引用即可;然后,开始配置web.config

    1.调用URLRewriter.dll

     <configSections>
        <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
      </configSections>

    2.配置伪静态规则,至于规则的配置,可取google上去查。

      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Rule1">
              <match url="^123_t([0-9]+).html$" ignoreCase="false" />
              <action type="Rewrite" url="Default.aspx?id={R:1}" appendQueryString="false" />
            </rule>
            <rule name="Rule2">
              <match url="^123.html" ignoreCase="false" />
              <action type="Rewrite" url="Default.aspx" appendQueryString="false" />
            </rule>
            <rule name="Rule3">
              <match url="^job/l-([0-9]+)-([0-9]+).html$" ignoreCase="false" />
              <action type="Rewrite" url="JobList.aspx?id={R:1}&amp;page={R:2}" appendQueryString="false" />
            </rule>
          </rules>
        </rewrite>
        <validation validateIntegratedModeConfiguration="false" />
      </system.webServer>

    补充:网上也有另一种配置方法,这里也贴下,引用的dll也是一样的,如下

    	<configSections>
    		<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
    	</configSections>
            <RewriterConfig>
    		<Rules>
    			<RewriterRule>
    				<LookFor>~/Index.html</LookFor>
    				<SendTo>~/Index.aspx</SendTo>
    			</RewriterRule>
    			<RewriterRule>
    				<LookFor>~/Index/t_([0-9]*)/ID_([0-9]*).html$</LookFor>//如:http://localhost:60948/Index/t_8/ID_456.html
    				<SendTo>~/Index.aspx?type=$1&id=$2</SendTo>s
    			</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" targetFramework="4.5"/>
    		<httpRuntime targetFramework="4.5"/>
    	</system.web>
    	<system.webServer>
    		<handlers>
    			<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler,URLRewriter" name="urlrewriter"/>
    		</handlers>
    		<validation validateIntegratedModeConfiguration="false" />
    	</system.webServer>
    

      

    3.问题

    每个人的电脑设置不同,可能会有各种不同的问题,但去百度基本能找到解决办法。本人对伪静态也是一知半解,大多也是查资料得来的信息,如有不同见解,望告知,谢谢。

  • 相关阅读:
    Cesium加载Geowebcache切片
    Vue开发--脚手架的搭建
    OpenLayers动态测量距离和面积,并可自定义测量的线样式
    OpenLayers要素拖拽
    改造SuperMap的DrawHandler接口,自定义绘制的图形样式
    Cesium动态绘制实体(点、标注、面、线、圆、矩形)
    ArcMap制图遇到的小问题
    GeoServer 2.15.2版本跨域问题解决方法
    MySQL 8.0 主从同步
    Service__cmd--MySQL安装并连接SQLyog
  • 原文地址:https://www.cnblogs.com/MrBlackJ/p/8018037.html
Copyright © 2011-2022 走看看