我在网站上的几个地方都有帮助文本,告诉用户允许的最大文件上传大小是多少。我希望能够使它具有动态性,这样,如果我更改了web.config文件中的请求限制,就不必在很多地方更改表单说明。是否可以使用ConfigurationManager或其他工具?
由于您没有提供更多详细信息:如此处所述,您有2个选项可以为整个应用程序设置大小限制。
取决于您需要采取一些不同的方法:
如果使用<httpRuntime maxRequestLength="" />
,则可以通过WebConfigurationManager获取信息
//The null in OpenWebConfiguration(null) specifies that the standard web.config should be opened
System.Configuration.Configuration root = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
var httpRuntime = root.GetSection("system.web/httpRuntime") as System.Web.Configuration.HttpRuntimeSection;
int maxRequestLength = httpRuntime.MaxRequestLength;
在Priciple中,您应该可以使用<requestLimits maxAllowedContentLength="" />
。但是system.webServer
,WebConfigurationManager中的-Section被声明为IgnoreSection,并且无法访问。可以在application.config或类似的IIS中更改此行为。但是由于(就我而言)甚至.SectionInformation.GetRawXml()
失败了,所以我倾向于宣布这是一个失败的案件。
在这种情况下,我的解决方案是手动访问Web.config-File:
var webConfigFilePath = String.Format(@"{0}Web.config", HostingEnvironment.MapPath("~"));
XDocument xml = XDocument.Load(System.IO.File.OpenRead(webConfigFilePath));
string maxAllowedContentLength = xml.Root
.Elements("system.webServer").First()
.Elements("security").First()
.Elements("requestFiltering").First()
.Elements("requestLimits").First()
.Attributes("maxAllowedContentLength").First().Value;
@Roman在此提出了另一种解决方案Microsoft.Web.Administration.ServerManager
,您需要使用Microsoft.Web.Administration软件包来使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句