在Web.Config的Location Path元素中指定多个目录

Jed*_*Jed 25 asp.net location web-config path

在我的ASP.NET的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>
Run Code Online (Sandbox Code Playgroud)

上面的示例指定除了两个目录dir1和dir2之外,所有目录都将被锁定到匿名用户.

我很好奇是否有我可以使用的语法,这将允许我在一个位置元素中定义多个目录.例如,如果我们可以做这样的事情会很方便......

  <location path="dir1,dir2,etc">
    <system.web>
      <authorization>
        <allow users="?"/>
      </authorization>
    </system.web>
  </location>
Run Code Online (Sandbox Code Playgroud)

Rya*_*hel 39

您不能在path属性中指定多个元素,但可以使用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>
Run Code Online (Sandbox Code Playgroud)

可以使用以下等效的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>
Run Code Online (Sandbox Code Playgroud)

allow.config

<?xml version="1.0"?>
<authorization>
  <allow users="*"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)

deny.config

<?xml version="1.0"?>
<authorization>
  <deny users="*"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)

随着每个部分中允许/拒绝规则的数量增加,此方法的有用性也会增加.


And*_*ich 15

抱歉,但path属性不允许使用","因此您必须为所有路径编写标记,或者您可以在每个目录中创建web.config.