部分配置部分的外部配置文件

时间:2012-04-16 11:51:16

标签: asp.net iis-7 web-config

我正在尝试使用外部配置文件在web.config文件的根location部分中定义多个<configuration>元素。这可能吗?

我理解如何为单个部分(例如connectionStrings)执行此操作,但是可以对配置元素中的多个元素执行此操作吗? <configuration>;本身不允许configSource属性。是否可以创建一个虚拟元素并在configSections中定义它,如果是这样我会给它什么类型?

背景信息:我想这样做,以便我可以定义永久重定向,可能有数百个,所以理想情况下我不想在web.config本身中定义它。即。

<configuration>
  <!-- rest of web.config here -->
  <!-- i need mutiple location elements so want these in an external file-->
  <location path="oldpage">
    <system.webServer>
      <httpRedirect enabled="true" destination="/uk/business" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
</configuration>

2 个答案:

答案 0 :(得分:1)

要做你想问的事你就需要这个:

<rewrite>
  <rules configSource="rewrites.config" />
</rewrite>

任何文件名都应该足够。

您可以在要外部化的元素上设置configSource属性。

答案 1 :(得分:1)

以为我会在此添加更多细节。正如CodeMonkey所提到的,URL Rewrite模块是可行的方法,有关重写模块http://www.iis.net/download/URLRewrite的更多详细信息。

这些是我对web.config定义规则和引用外部文件所做的更改(在system.webServer中)

<rewrite>
  <rewriteMaps configSource="RewriteMaps.config" />                
  <rules>
      <rule name="Rewrite rule1 for StaticRewrites">
          <match url=".*" />
          <conditions>
              <add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
          <action type="Redirect" url="{C:1}" redirectType="Permanent" appendQueryString="false" />
      </rule>
  </rules>        
</rewrite>

注意:还需要更新VS XML Schema以阻止它呻吟这么多 http://ruslany.net/2010/04/visual-studio-xml-intellisense-for-url-rewrite-2-0/ (请确保您在此博客上看到评论重新更新vs2010的js)