static void

ASP 2.0 Configuration

AppSettings and Config Settings

To use ConfigurationManager you have to reference System.Configuration.

AppSettings

WebConfigurationManager.AppSettings["TextSetting"]
or <%$ AppSettings: TextSetting %>

Config Settings

WebConfigurationManager.ConnectionStrings["pubs"].ConnectionString
or <%$ ConnectionStrings: pubs %>

//Modify configuration
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection appSettings = config.GetSection("appSettings") as AppSettingsSection;
bool found = false;
foreach(string key in appSettings.Settings.AllKeys) //iterate
{
    if (key == "Key1")
    {
        found = true;
        break;
    }
}
if (!found) appSettings.Settings.Add("Key1", "");
appSettings.Settings["Key1"].Value = "New Value";
try
{
    config.Save(); //if you have write access- by default you won't!
}
catch (ConfigurationErrorsException)
{
    Label1.Text = "ASP.NET process account (ASPNET or Network Service) must have write permission granted for the Web.config file";
}

Encrypting/decrypting AppSettings and ConnectionStrings

Use command line aspnet_regiis with -pe (encrypt) and -pd (decrypt) switches

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" c:\inetpub\wwwroot

.Net4: %windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe

For others to use it, you have to export (-pri as you need both private and public key)

From code WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
.GetSection("ConnectionStrings") has SectionInformation with ProtectSection(null) and UnProtectSection - default is RSA provider, alternatively use DPAPI. Then config.Save()

You can also use this for app.Configs (rename them to web.config while running the option, then rename back). Use the -pef flag (for file paths) not -pe (uses IIS).

Email

For a named server

<system.net>
    <mailSettings>
        <smtp from="[email protected]">
            <network host="mail.domain.com" port="25" userName="" password="" defaultCredentials="true"/>
        </smtp>
    </mailSettings>
</system.net>

For IIS pickup

<system.net>
    <mailSettings>
        <smtp deliveryMethod="PickupDirectoryFromIis">
            <specifiedPickupDirectory pickupDirectoryLocation="C:\Inetpub\mailroot\Pickup"/>
        </smtp>
    </mailSettings>
</system.net>

Profiles

Health Monitoring

Web.config has eventMappings (:WebManagementEvent), providers (:WebEventProvider) and links eventName to provider by rules

For Sql logging aspnet_regsql.exe -U user -P pw -S server -A w

For email and non-SQLExpress error logging (in system.web). This disables the EventLog.

<healthMonitoring enabled="true">
    <providers>
        <add name="MailEventProvider" type="System.Web.Management.SimpleMailWebEventProvider" from="[email protected]" to="[email protected]" bodyHeader="An error occurred" bodyFooter="Health Monitoring Provider" subjectPrefix="Error recorded." maxEventLength="4096" maxMessagesPerNotification="1" buffer="false"/>
        <!-- Use real SQLServer, not default SQL Express -->
        <remove name="SqlWebEventProvider"/>
        <add name="SqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" connectionStringName="ConnectionString" maxEventDetailsLength="1073741823" buffer="false"/>
    </providers>
    <rules>
        <!-- All Errors Default is defined in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\Web.config and uses EventLogProvider-->
        <remove name="All Errors Default"/>
        <add name="All Errors to SQL" eventName="All Errors" provider="SqlWebEventProvider" profile="Default" minInterval="00:00:30"/>
        <add name="All Errors To Email" eventName="All Errors" provider="MailEventProvider"/>
    </rules>
</healthMonitoring>

See health monitoring to text file code

Use .Net 3.5

When you upgrade a web.config from 2.0 to 3.5, copy-paste in the following - system.codedom:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
              type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <providerOption name="CompilerVersion" value="v3.5"/>
      <providerOption name="WarnAsError" value="false"/>
    </compiler>
  </compilers>
</system.codedom>

Also reference the 3.5 assemblies in system.web

<system.web>
    <compilation debug="false">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
  <pages>
    <controls>
      <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </controls>
  </pages>
  <httpHandlers>
    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
  </httpHandlers>
  <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </httpModules>
</system.web>

File Upload sizes

IIS default is 4Mb. To increase to 100Mb you must set it in 2 places (ASP + IIS 7+):

<system.web>
  <httpRuntime targetFramework="4.5" maxRequestLength="1048576" />
</system.web>
 
<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>