在URL重写规则中访问特定的应用程序键



我正在尝试访问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规则中的APIURL键

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

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

  • 使用MSBuild社区任务项目中的XMLUPDATE任务来更新配置文件。我的工作已经在使用它,所以这是我走的道路。看起来像:

    <XmlUpdate 
      XPath="//rule[@name='ProxyApi']/action/@url"
      XmlFileName="{Your Config File Location}"
      Value="https://www.x.com/api/{R:1}" />
    
  • 使用xsltransformation任务更新您的配置文件。该解决方案是内置的,但需要更多的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>
    

相关内容

最新更新