6

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# ?

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
Farhan
  • 61
  • 1
  • 2

2 Answers2

3

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.

nairdo
  • 81
  • 6
3

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).

Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191