asp.net - 如何读取 maxAllowedContentLength

标签 asp.net iis-7 file-upload uploadify

对于我的网络应用程序,我有用于上传文件的 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/

相关文章:

jquery - 如何从 jquery 回调将参数传递给 Controller ​​操作方法?

c# - 如何在代码隐藏中访问服务器端 div(内容页面内部)

asp.net - 手机访问本地IIS网站

asp.net - 有什么方法可以从 ASP.NET 访问 IIS 内核缓存?

perl - www::curl - 如何上传(发布)大文件

c# - 如何在不使用 API 的情况下通过 IP 地址查找位置

http - 我可以通过 1 个 IP 地址在 LAN 上托管两个独立的 Web 服务器到外部世界吗?

asp.net - IIS 应用程序池标识以使用登录用户 Windows 标识

java - 在 java 中公开 SOAP 端点以进行文件上传

mysql - 上传 Coldfusion 网站