Page Specific Configuration in Web.Config

2019-07-21 08:07发布

问题:

I do wonder whether there is a way to set page-based configuration in web.config? Lets say I have Default.aspx and Product.aspx and I want to define different configurations such as EnableViewState etc. but in web.config rather than in page itself.

So you may ask why? Please do understand this is how it should be.

回答1:

You can use the location tag in web.config to specify different settings for different paths.

<configuration>
   <location path="Logon.aspx">
      <system.web>
         <authorization>
            <allow users="?"/>
         </authorization>
      </system.web>
   </location>

   <location path="UploadPage.aspx">
     <system.web>
       <httpRuntime maxRequestLength="128"/>
    </system.web>
  </location>
</configuration>


回答2:

I'm not sure if this is the only solution, but placing the page in its own folder and defining a web.config in that folder would allow you to achieve per-page configuration. Combine this with custom routing and it wouldn't be completely horrible.