【发布时间】:2011-10-16 20:25:17
【问题描述】:
如何在 ASP.NET 中使用 C# 作为代码以编程方式设置(使用 GET SET 属性)“httpRuntime maxRequestLength”
有没有办法通过 C# 在web.config 中设置值?
【问题讨论】:
标签: c# asp.net web-config code-behind httpruntime
如何在 ASP.NET 中使用 C# 作为代码以编程方式设置(使用 GET SET 属性)“httpRuntime maxRequestLength”
有没有办法通过 C# 在web.config 中设置值?
【问题讨论】:
标签: c# asp.net web-config code-behind httpruntime
可以,可以在web.config中设置
只需在<system.web> 部分下的web.config 中设置即可。在下面的示例中,我将maximum length 设置为2GB
<httpRuntime maxRequestLength="2097152" executionTimeout="600" />
请注意maxRequestLength 设置在KB's 中,最大可设置为2GB (2079152 KB's)。但默认文件大小限制为(4MB)。
【讨论】:
您可以像这样在代码中设置 web.config 的 maxRequestLength 属性:
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();
我们在Rock RMS 内容管理系统中使用this exact thing。
【讨论】: