【问题标题】:Rewrite Rules in separate config file在单独的配置文件中重写规则
【发布时间】:2019-09-17 15:58:37
【问题描述】:

我在我的应用程序中使用 URL 重写,我有两个配置文件,如下所示,第一个具有配置,第二个具有规则。但我收到 404 错误。

网络配置

  <system.webServer>
    <rewrite>
      <rewriteMaps configSource="rewritemaps.config"></rewriteMaps>
    </rewrite>
  </system.webServer>

rewritemaps.config 文件

<?xml version="1.0"?>
<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>

  <system.webServer>
    <rewrite>
      <rules> 

        <rule name="RewriteURLHometPaging" stopProcessing="true">
          <match url="^Home" />
          <action appendQueryString="false" type="Rewrite" url="Default.aspx" />
        </rule>
        <rule name="RedirectURLHomePaging" stopProcessing="true">
          <match url="^Default\.aspx$" />
          <action appendQueryString="false" type="Redirect" url="Home" />
        </rule>

        <rule name="RedirectURLContactPaging" stopProcessing="true">
          <match url="^Contact-Us\.aspx$" />
          <action appendQueryString="false" type="Redirect" url="Contactus" />
        </rule>

        <rule name="RewriteURLContactPaging" stopProcessing="true">
          <match url="^Contactus" />
          <action appendQueryString="false" type="Rewrite" url="Contact-Us.aspx" />
        </rule>

        <rule name="RedirectURLAboutPaging" stopProcessing="true">
          <match url="^About-Us\.aspx$" />
          <action appendQueryString="false" type="Redirect" url="About" />
        </rule>

        <rule name="RewriteURLAboutPaging" stopProcessing="true">
          <match url="^About" />
          <action appendQueryString="false" type="Rewrite" url="About-Us.aspx" />
        </rule>

      </rules>
    </rewrite>
  </system.webServer>

</configuration>

错误截图在这里:

【问题讨论】:

    标签: asp.net configuration url-rewriting web-config


    【解决方案1】:

    多个配置文件

    appSettings 元素可以包含指向外部文件的文件属性。它会将 web.config 文件更改为如下所示:

    <appSettings/>
    
    <connectionStrings/>
    
    <system.web>
    
        <compilation debug="false" strict="false" explicit="true" />
    
    </system.web>
    
    <appSettings file="externalSettings.config"/>
    
    </configuration>
    

    【讨论】:

      【解决方案2】:

      以下是如何使用一个或多个重写映射创建外部文件(来源 ruslany.net)。

      您还可以使用此技术为重写规则创建外部文件。请参阅上面链接的 ruslany.net 上的完整帖子的结尾。

      Web.config:

      <configuration>
      <system.webServer>
        <rewrite>
          <rewriteMaps configSource="MyRewriteMaps.config"></rewriteMaps>
          <rules>
            <rule name="Redirect rule for MyRedirects">
              <match url=".*" />
              <conditions>
                <add input="{MyRedirects:{REQUEST_URI}}" pattern="(.+)" />
              </conditions>
              <action type="Redirect" url="{C:1}" appendQueryString="false" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
      </configuration>
      

      MyRewriteMaps.config:

      <rewriteMaps>
        <rewriteMap name="MyRedirects">
          <add key="/oldurl" value="/newurl" />
          <add key="/otheroldurl" value="/othernewurl" />
        </rewriteMap>
      </rewriteMaps>
      

      【讨论】:

        猜你喜欢
        • 2021-09-24
        • 1970-01-01
        • 2013-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-25
        • 2016-06-06
        • 1970-01-01
        相关资源
        最近更新 更多