【问题标题】:Access specific appsettings key in Url Rewrite rules访问 URL 重写规则中的特定 appsettings 键
【发布时间】:2017-08-30 00:01:14
【问题描述】:

我正在尝试访问 URL 重写规则中的 AppSettings 键,但我不确定如何访问它们。谁能帮帮我?

<appSettings>
  <add key="APIUrl" value="https://www.x.com/api/{R:1}" />
</appSettings>
<system.webServer>  
 <rewrite>
  <rules>
    <rule name="ProxyApi" stopProcessing="true">
      <match url="^api/?(.*)" />
      <serverVariables>
        <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
        <set name="HTTP_X_ORIGINAL_HOST" value="{HTTP_HOST}" />
      </serverVariables>
      <action type="Rewrite" url="{APIUrl}" />
    </rule>
  </rules>
 </rewrite>
</system.webServer>

尝试访问 UrlRewrite Rule 中的 APIUrl 键

【问题讨论】:

  • 您找到解决方案了吗?我也是这种情况

标签: url-rewriting web-config web.config-transform


【解决方案1】:

我认为 appsettings 在您的配置文件中的其他位置不可用。

我找到了两种使用 msbuild 解决此问题的方法:

  • 使用来自MSBuild Community Tasks Project 的xmlupdate 任务来更新配置文件。我的工作已经在使用它,所以这就是我所走的路。看起来像:

    <XmlUpdate 
      XPath="//rule[@name='ProxyApi']/action/@url"
      XmlFileName="{Your Config File Location}"
      Value="https://www.x.com/api/{R:1}" />
    
  • 使用XslTransformation Task 更新您的配置文件。这个解决方案是内置的,但需要更多的 XSL 知识。 Xsl 看起来像:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    
      <!-- identity transform -->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="//rule[@name='ProxyApi']/action/@url">
        <xsl:attribute name="url">
          <xsl:value-of select="'https://www.x.com/api/{R:1}'"/>
        </xsl:attribute>
      </xsl:template>
    
    </xsl:stylesheet>
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2015-10-23
    • 2012-04-27
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    相关资源
    最近更新 更多