对于我的网络应用程序,我有用于上传文件的 Flash 组件。我想在客户端处理最大文件大小限制,而无需将该文件实际发送到服务器。所以我需要以某种方式从配置文件中读取该值以将其发送给客户端。我发现的一些文章说直接读取配置文件不是解决方案,因为它可以在很多地方进行更改。所以应该有一些API调用,但我找不到任何......
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1048576" />
</requestFiltering>
</security>
</system.webServer>
最佳答案
我知道这是一个老问题,但它花费了我太多时间(浪费了),以至于我想为那些可能会遇到我的人发布一个可行的解决方案:
Using Microsoft.Web.Administration;
uint uiMaxAllowedContentLength = 0;
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetWebConfiguration("Default Web Site/{{your special site}}");
ConfigurationSection requestFilteringSection = config.GetSection("system.webServer/security/requestFiltering");
ConfigurationElement requestLimitsElement = requestFilteringSection.GetChildElement("requestLimits");
object maxAllowedContentLength = requestLimitsElement.GetAttributeValue("maxAllowedContentLength");
if (null != maxAllowedContentLength)
{
uint.TryParse(maxAllowedContentLength.ToString(), out uiMaxAllowedContentLength);
}
}
确保首先下载并安装 Microsoft Web Administration 包
(
PM> Install-package Microsoft.Web.Administration
)
此外,您可能需要调整对 web.config 文件的权限。至少授予 IUSR 和 IIS_IUSRS“读取”权限。
该代码实际上来自 Microsoft 网站,但找到它花了很长时间!希望我为您节省了几个小时。
干杯,
罗马
关于asp.net - 如何读取 maxAllowedContentLength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8443574/