2011-01-05 30 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