如何阅读maxAllowedContentLength

Fre*_*dyC 4 asp.net iis-7 file-upload uploadify

对于我的网络应用程序,我有用于上传文件的flash组件.我想在客户端处理最大文件大小限制而不实际将该文件发送到服务器.所以我需要以某种方式从配置文件中读取该值以将其发送到客户端.我发现的一些文章说直接读取配置文件不是解决方案,因为它可以在很多地方进行更改.所以应该有一些API调用,但我找不到任何...

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="1048576" />
        </requestFiltering>
    </security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

Rom*_*man 7

我知道这是一个古老的问题,但是我花了很多时间(浪费了),我觉得为那些可能会走在我路上的人发布一个有效的解决方案:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

确保首先下载并安装Microsoft Web Administration包(

PM>安装包Microsoft.Web.Administration

)

此外,您可能需要调整web.config文件的权限.给IUSR和IIS_IUSRS至少"读取"权限.

该代码实际上来自微软网站,但发现它需要永远!希望我已经为你节省了几个小时.

干杯,

罗马