【问题标题】:How to read maxAllowedContentLength如何阅读 maxAllowedContentLength
【发布时间】:2012-01-16 15:10:35
【问题描述】:

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

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="1048576" />
        </requestFiltering>
    </security>
</system.webServer>

【问题讨论】:

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


    【解决方案1】:

    我知道这是一个老问题,但它花费了我很多时间(浪费),我想为那些可能会遇到我的人发布一个可行的解决方案:

    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 管理包 (

    PM> 安装包 Microsoft.Web.Administration

    )

    此外,您可能需要调整对 web.config 文件的权限。至少授予 IUSR 和 IIS_IUSRS“读取”权限。

    代码实际上来自 Microsoft 网站,但找到它花了很长时间!希望我为您节省了几个小时。

    干杯,

    罗马

    【讨论】:

      【解决方案2】:

      没有 Microsoft Web 管理包:

      using System.Web.Configuration;
      using System.Configuration;
      using System.Xml;
      using System.IO;
      
      Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
      IgnoreSection ignoreSection = configuration.GetSection("system.webServer") as IgnoreSection;
      string sectionXml = ignoreSection.SectionInformation.GetRawXml();
      StringReader stringReader = new StringReader(sectionXml);
      XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
      UInt32 maxAllowedContentLength = 0;
      if(xmlTextReader.ReadToDescendant("requestLimits"))
          UInt32.TryParse(xmlTextReader.GetAttribute("maxAllowedContentLength"), out maxAllowedContentLength);
      

      【讨论】:

        【解决方案3】:

        试试这个方法

        您可以根据配置文件中的Web配置更改以下代码段

        我的 web.config 一些看起来像

        <system.web>
          <httpRuntime executionTimeout="30"  maxRequestLength="100"/>
        

        在这里你可以看到maxRequestLength被定义为100,可以从页面后面的代码中更改

        添加using System.Web.Configuration;

        现在写这段代码来改变maxRequestLength的值

        Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
        HttpRuntimeSection httpruntinesec =
            (HttpRuntimeSection)configuration.GetSection("system.web/httpRuntime");
        

        您可以使用httpruntinesce 实例设置该值。

        【讨论】:

        • 我不确定我是否应该否决这个答案,因为这是为了阅读“maxRequestLength”(来自system.web/httpRuntime)而不是“maxAllowedContentLength”(来自system.webServer/security/requestFiltering/requestLimits
        • 我认为没问题,因为可以将两个设置 (maxRequestLength && maxAllowedContentLength) 设置为相应的(相同)值,并且只使用此代码跳过安装“Microsoft.Web.Administration”包。跨度>
        猜你喜欢
        • 1970-01-01
        • 2018-02-20
        • 1970-01-01
        • 2011-09-08
        • 2011-01-19
        • 2018-06-11
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多