2011-01-05 35 views
22

在我的ASP.NET的Web配置文件我有以下的位置元素定義了多個目錄:Web.Config中的位置路徑元素指定

<location path=""> 
    <system.web> 
     <authorization> 
     <deny users="?"/> 
     </authorization> 
    </system.web> 
    </location> 

    <location path="dir1"> 
    <system.web> 
     <authorization> 
     <allow users="?"/> 
     </authorization> 
    </system.web> 
    </location> 

    <location path="dir2"> 
    <system.web> 
     <authorization> 
     <allow users="?"/> 
     </authorization> 
    </system.web> 
    </location> 

上面的例子規定所有目錄將被鎖定下來除了兩個目錄dir1和dir2以外的匿名用戶。

我很好奇,如果有一種語法,我可以使用,這將允許我在一個位置元素內定義多個目錄。例如,這將是方便,如果我們可以做這樣的事情...

<location path="dir1,dir2,etc"> 
    <system.web> 
     <authorization> 
     <allow users="?"/> 
     </authorization> 
    </system.web> 
    </location> 
+2

看起來它過去被記錄爲允許以逗號分隔的路徑列表,但它們修復了文檔而不是實現記錄的功能。 http://connect.microsoft.com/VisualStudio/feedback/details/104010/location-path-attribute-in-web-config-doesnt-accept-multiple-paths – Triynko 2011-03-30 18:44:36

+0

@Triynko https://connect.microsoft.com/ VisualStudio/feedback/details/104010/location-path-attribute-in-web-config-doesnt-accept-multiple-paths not found – Kiquenet 2015-07-06 07:24:53

回答

14

抱歉,但路徑屬性不允許使用「」 所以你必須寫標籤的所有路徑, 或者您可以在每個目錄中創建web.config。

34

您不能在路徑屬性中指定多個元素,但可以使用configSource屬性。

例如,下面原來的web.config文件:

<?xml version="1.0"?> 
<configuration> 
    <location path="form1.aspx"> 
    <system.web> 
     <authorization> 
     <allow users="*"/> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="form2.aspx"> 
    <system.web> 
     <authorization> 
     <allow users="*"/> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="form3.aspx"> 
    <system.web> 
     <authorization> 
     <allow users="*"/> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="form4.aspx"> 
    <system.web> 
     <authorization> 
     <deny users="*"/> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="form5.aspx"> 
    <system.web> 
     <authorization> 
     <deny users="*"/> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="form6.aspx"> 
    <system.web> 
     <authorization> 
     <deny users="*"/> 
     </authorization> 
    </system.web> 
    </location> 
</configuration> 

可以通過以下等價的web.config,allow.config和deny.config文件被替換:

的web.config

<?xml version="1.0"?> 
<configuration> 
    <location path="form1.aspx"> 
    <system.web> 
     <authorization configSource="allow.config" /> 
    </system.web> 
    </location> 
    <location path="form2.aspx"> 
    <system.web> 
     <authorization configSource="allow.config" /> 
    </system.web> 
    </location> 
    <location path="form3.aspx"> 
    <system.web> 
     <authorization configSource="allow.config" /> 
    </system.web> 
    </location> 
    <location path="form4.aspx"> 
    <system.web> 
     <authorization configSource="deny.config" /> 
    </system.web> 
    </location> 
    <location path="form5.aspx"> 
    <system.web> 
     <authorization configSource="deny.config" /> 
    </system.web> 
    </location> 
    <location path="form6.aspx"> 
    <system.web> 
     <authorization configSource="deny.config" /> 
    </system.web> 
    </location> 
</configuration> 

allow.config

<?xml version="1.0"?> 
<authorization> 
    <allow users="*"/> 
</authorization> 

deny.config

<?xml version="1.0"?> 
<authorization> 
    <deny users="*"/> 
</authorization> 

的這種方法隨着允許/拒絕在每個部分中的增加規則的數量的有用性。

+0

我能否進一步簡化,只有配置了configSource的位置線?所以它基本上只是一個位置列表? – Ray 2015-04-06 21:02:19

+0

@射線:當我嘗試時,我得到了'無法識別的屬性'configSource''。 – 2016-07-06 18:31:03