Search code examples
c#asp.netweb-config

Apply web config settings for a specific page only


I have the following config that I would like to remove from web config and only enforce in code for a certain page 'Page1.aspx', else the configuration would apply across all pages.

How would I specify this configuration or enforce it through code, so it applies to only Page1.aspx?

<system.web>
  <browserCaps>
    <case>
        RequiresControlStateInSession=true
    </case>
  </browserCaps>
</system.web>

Solution

  • I just found a way to mention configuration on a page basis, so the configuration only applies to that page.

    Just include any specific configuration under a <location> element that you would like to apply to a single page. Keep the following points in mind when using this approach:

    • Set an appropriate value for the path attribute of the <location> element. It can be a relative path.
    • The <location> element must be outside the <system.web>, <system.webserver>, and any other sections within Web.config.

    In my case the following worked, where I specified a <location> section for the single page for which I wanted to specify special configuration:

    <?xml version="1.0"?>
    <configuration>
    <!-- all sections in web config go here. Put the Location elements always 
     at end of your web config file -->
      <location path="Page1.aspx">
       <system.web>
        <browserCaps>
         <case>
           RequiresControlStateInSession=true
         </case>
        </browserCaps>
       </system.web>
      </location>
    </configuration>