
我有一個網站需要使用預設應用程式根目錄的文檔,但不能用於子目錄。我正在使用“目錄瀏覽”功能,但我只希望它適用於子目錄。
這是我的 web.config 的一個片段:
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
<add value="1f200f74-07e5-4681-a275-e9cbc9f1b794.txt" />
</files>
</defaultDocument>
<directoryBrowse enabled="true" />
我嘗試申請繼承InChildApplications到 defaultDocument 元素,但這不起作用,實際上它會使應用程式失敗。所以我希望defaultDocument只套用於根目錄,而我想將directoryBrowse套用到所有子目錄。
我意識到我可以在每個直接子目錄中都有 web.config 文件,並對 defaultDocument 應用清除,但我不能將 web.config 文件放在這些目錄中(此應用程式的性質不允許這樣做)。 (也,允許子目錄配置在這裡不起作用,這只會阻止加工子目錄中的 web.config 檔案。
web.config 中是否有配置將設定套用至該節點僅有的?
答案1
解決方案是使用 位置標籤:
位置標記用於指定特定於路徑的配置,作為將資料夾中的 web.config 檔案對應到該虛擬路徑的替代方法。路徑的位置標記在配置層次結構的父層級中設置,並被視為位於該父層級。
請參閱上面連結中的範例。
答案2
根據harrymc的回答,我能夠讓這個配置運作:
<location path="." inheritInChildApplications="false">
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear />
<add value="1f200f74-07e5-4681-a275-e9cbc9f1b794.txt" />
</files>
</defaultDocument>
</system.webServer>
</location>
<system.webServer>
<!--<defaultDocument enabled="true">
<files>
<clear />
<add value="1f200f74-07e5-4681-a275-e9cbc9f1b794.txt" />
</files>
</defaultDocument>-->
<directoryBrowse enabled="true" />
...
的使用繼承InChildApplications屬性中的地點元素也是必要的。我保留了原始的“defaultDocument”元素,但將其註釋為我最初嘗試的佔位符。