Farhan
Farhan

Reputation: 61

How to programmatically set (using GET SET property) "httpRuntime maxRequestLength" in ASP.NET with C# as code behind

How to programmatically set (using GET SET property) "httpRuntime maxRequestLength" in ASP.NET with C# as code behind

is there any way to set values in web.config through C# ?

Upvotes: 6

Views: 3459

Answers (2)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

Yes, you can set it in the web.config

Just set it in the web.config under the <system.web> section. In the below example I am setting the maximum length to 2GB

<httpRuntime maxRequestLength="2097152" executionTimeout="600" />

Please note that the maxRequestLength is set in KB's and it can be set up to 2GB (2079152 KB's). But default file size limit is (4MB).

Upvotes: 3

nairdo
nairdo

Reputation: 81

You can set the web.config's maxRequestLength property in code like this:

Configuration webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( "~" );
var section = (System.Web.Configuration.SystemWebSectionGroup)webConfig.GetSectionGroup("system.web");
section.HttpRuntime.MaxRequestLength = 10 * 1024; // 10 MB
webConfig.Save();

We do this exact thing in our Rock RMS content management system.

Upvotes: 3

Related Questions