如何在web.config中指定文件路径以检查文件是否存在? [英] How to specify the file path in web.config for checking file existence?

查看:53
本文介绍了如何在web.config中指定文件路径以检查文件是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A website's file directory looks like the following:

在IIS中.该网站的目录设置为C:\ mywebiste,并绑定端口8086.可以毫无问题地浏览以下网页:

In IIS. the website's directory is set to C:\mywebiste, and is bound port 8086. The following web page can be browsed without any problem:

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

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

I want to use IIS rewrite to use the compressed foo.dat ONLY if it exists, so the web.config looks like the following:

<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我把它们放在哪里.此问题是

It works fine. Since I may distribute the contents under folder dist with the corresponding web.config in any directory, I am wondering if there is a parameter that can replace "{APPL_PHYSICAL_PATH}home\dist" so that I can use the same web.config no matter where I place them. This question is an extension from another similar question per the suggestion of the kind answer provider.

2019-10-03 基于出色的评论,我现在可以为所有文件重写:

2019-10-03 Based on the excellent well commented answer, I can rewrite for all the files now:

<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 文件夹下的版本.

If I understood you correctly, you always want to rewrite the version under the _compressed_br folder next to the web.config.

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

If so, give the following a try:

<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>

这篇关于如何在web.config中指定文件路径以检查文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆