Essential web.config Settings for ASP.NET 4.x
Configure your ASP.NET Framework 4.x application properly for Plesk Windows hosting.
Basic web.config Structure
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Setting1" value="Value1" />
</appSettings>
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=localhost;Database=myDb;User Id=user;Password=pass;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="false" targetFramework="4.8" />
<httpRuntime targetFramework="4.8" maxRequestLength="32768" />
<customErrors mode="RemoteOnly" defaultRedirect="~/Error" />
</system.web>
</configuration>
Custom Error Pages
<system.web>
<customErrors mode="RemoteOnly">
<error statusCode="404" redirect="~/Errors/NotFound" />
<error statusCode="500" redirect="~/Errors/ServerError" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="/Errors/NotFound" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
Authentication Settings
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="30" slidingExpiration="true" />
</authentication>
<authorization>
<deny users="?" /> <!-- Deny anonymous -->
</authorization>
</system.web>
Session State
<system.web>
<sessionState mode="InProc" timeout="20" />
</system.web>
Machine Key (Required for Web Farms/Load Balancing)
<system.web>
<machineKey
validationKey="YOUR_VALIDATION_KEY"
decryptionKey="YOUR_DECRYPTION_KEY"
validation="SHA1"
decryption="AES" />
</system.web>
Tip: Generate keys using IIS Manager or online tools.
URL Rewrite (HTTPS Redirect)
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
SMTP Email Settings
<system.net>
<mailSettings>
<smtp from="noreply@yourdomain.com">
<network host="mail.yourdomain.com"
port="587"
userName="user@yourdomain.com"
password="yourpassword"
enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
Increase Upload Limits
<system.web>
<httpRuntime maxRequestLength="51200" /> <!-- 50MB in KB -->
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="52428800" /> <!-- 50MB in bytes -->
</requestFiltering>
</security>
</system.webServer>
Common Issues
- Yellow Screen of Death: Set customErrors mode="On" for production
- ViewState errors: Add machineKey for consistent encryption
- Upload fails: Increase maxRequestLength and maxAllowedContentLength
- Session lost: Check sessionState timeout and Application Pool recycling