Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在单独的配置文件中重写规则_Asp.net_Configuration_Url Rewriting_Web Config - Fatal编程技术网

Asp.net 在单独的配置文件中重写规则

Asp.net 在单独的配置文件中重写规则,asp.net,configuration,url-rewriting,web-config,Asp.net,Configuration,Url Rewriting,Web Config,我在应用程序中使用URL重写,我有两个配置文件,如下所示,第一个有配置,第二个有规则。但我得到了404错误 网络配置 <system.webServer> <rewrite> <rewriteMaps configSource="rewritemaps.config"></rewriteMaps> </rewrite> </system.webServer> 重写maps.confi

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

网络配置

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

重写maps.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>


多个配置文件

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

<appSettings/>

<connectionStrings/>

<system.web>

    <compilation debug="false" strict="false" explicit="true" />

</system.web>

<appSettings file="externalSettings.config"/>

</configuration>


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

您还可以使用此技术为重写规则创建外部文件。请参阅上面链接的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>
<rewriteMaps>
  <rewriteMap name="MyRedirects">
    <add key="/oldurl" value="/newurl" />
    <add key="/otheroldurl" value="/othernewurl" />
  </rewriteMap>
</rewriteMaps>

MyRewriteMaps.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>
<rewriteMaps>
  <rewriteMap name="MyRedirects">
    <add key="/oldurl" value="/newurl" />
    <add key="/otheroldurl" value="/othernewurl" />
  </rewriteMap>
</rewriteMaps>