Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 403
Farhan Avatar asked Jul 31 '11 09:07

Farhan


People also ask

What is maxRequestLength for HttpRuntime?

HttpRuntime maxRequestLength The default size is 4096 kilobytes (4 MB). Max value 2,147,483,647 kilobytes (~82 Terabyte).

What is maxRequestLength?

The MaxRequestLength property specifies the limit for the buffering threshold of the input stream. For example, this limit can be used to prevent denial of service attacks that are caused by users who post large files to the server.

What property we need to set while uploading data more than 4 MB?

You have to increase the maxRequestLength setting in web.


1 Answers

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.

like image 162
nairdo Avatar answered Sep 19 '22 14:09

nairdo