如何在web.config中指定文件路径来检查文件是否存在?

How to specify the file path in web.config for checking file existence?

一个网站的文件目录如下所示:

在 IIS 中。网站目录设置为C:\mywebiste,绑定8086端口,可以正常浏览以下网页:

https://localhost:8086/home/dist/

我想使用 IIS 重写来使用压缩的 foo.dat ONLY 如果它存在,所以 web.config 如下所示:

<rule name="foo" stopProcessing="true">
  <match url="foo.dat$"/>
  <conditions>
    <!-- Match brotli requests -->
    <add input="{HTTP_ACCEPT_ENCODING}" pattern="br" />
    <!-- Check if the pre-compressed file exists on the disk -->
    <add input="{APPL_PHYSICAL_PATH}home\dist\_compressed_br\foo.dat" matchType="IsFile" negate="false" />
  </conditions>
  <action type="Rewrite" url="_compressed_br/foo.dat" />
</rule>

它工作正常。由于我可能会将dist文件夹下的内容与对应的web.config分发到任何目录,所以我想知道是否有一个参数可以替换“{APPL_PHYSICAL_PATH}home\dist”这样我就可以无论我将它们放在哪里,都使用相同的 web.config。根据友善的回答提供者的建议,这个问题是 的扩展。

[编辑] 2019-10-03 基于评论很好的答案,我现在可以重写所有文件:

<rule name="Rewrite br" stopProcessing="true">
  <match url="^(.*)$"/>
  <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
    <!-- Match brotli requests -->
    <add input="{HTTP_ACCEPT_ENCODING}" pattern="br" />
    <!-- following condition captures a group {C:1} with the value "C:\some\directory" for a path "c:\some\directory\foo.dat" -->
    <!-- so we can use in the next condition to check whether the compressed version exists -->
    <add input="{REQUEST_FILENAME}" pattern="^(.*)\([^\]*)$"/>
    <!-- Check if the pre-compressed file exists on the disk -->
    <!-- {C:1} used as requested file's parent folder -->
    <add input="{C:1}\_compressed_br\{C:2}" matchType="IsFile"/>
    </conditions>
  <action type="Rewrite" url="_compressed_br/{C:2}" />
  <serverVariables>
    <set name="RESPONSE_Content-Encoding" value="br"/>
  </serverVariables>
</rule>   

如果我没理解错的话,你总是想在 web.config 旁边的 _compressed_br 文件夹下重写版本。

如果是这样,请尝试以下操作:

<rule name="foo" stopProcessing="true">
    <match url="^foo\.dat$"/>
    <!-- trackAllCaptures="true" is required to capture regex groups across conditions -->
    <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
        <add input="{HTTP_ACCEPT_ENCODING}" pattern="br"/>
        <!-- following condition captures a group {C:1} with the value "C:\some\directory" for a path "c:\some\directory\foo.dat" -->
        <!-- so we can use in the next condition to check whether the compressed version exists -->
        <add input="{REQUEST_FILENAME}" pattern="^(.*)\foo\.dat$"/>
        <!-- {C:1} used as requested file's parent folder -->
        <add input="{C:1}\_compressed_br\foo.dat" matchType="IsFile"/>
    </conditions>
    <action type="Rewrite" url="_compressed_br/foo.dat"/>
    <serverVariables>
        <set name="RESPONSE_Content-Encoding" value="br"/>
    </serverVariables>
</rule>