【问题标题】:How to get the value from attribute "maxAllowedContentLength" of web.config programmatically?如何以编程方式从 web.config 的属性“maxAllowedContentLength”获取值?
【发布时间】:2018-03-14 16:59:19
【问题描述】:

我需要访问 web.config 文件中的 system.webServer/security/requestFiltering/requestLimits 部分,以获取属性 maxAllowedContentLength 的值。验证配置需要此值,因此用户不能设置比 web.config 文件中定义的值更高的值。要验证此配置,还需要属性 maxRequestLength (system.web/httpRuntime) 中的值,但我们已经通过以下代码获取该值:

(ConfigurationManager.GetSection("system.web/httpRuntime") as System.Web.Configuration.HttpRuntimeSection).MaxRequestLength

我已经试过了:

  • (ConfigurationManager.GetSection("system.webServer") as IgnoreSection).SectionInformation.GetRawXml(),但它会抛出 System.InvalidOperationException

  • (System.Web.Configuration.WebConfigurationManager.GetSection("system.webServer") as IgnoreSection).SectionInformation.GetRawXml(),但它也会抛出一个System.InvalidOperationException

  • ConfigurationManager.GetSection("system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength"),但它返回null

  • System.Web.Configuration.WebConfigurationManager.GetSection("system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength"),但它也返回null

  • DiskJunky 建议的ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None),但它会抛出 System.ArgumentException,并显示消息“不在独立 exe 中运行时必须指定 exePath”。

另外,我做了下面的代码:

using (System.IO.StreamReader reader = new System.IO.StreamReader(System.Web.HttpRuntime.AppDomainAppPath + "/web.config"))
{
    System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
    xmlDocument.LoadXml(reader.ReadToEnd());

    if (xmlDocument.GetElementsByTagName("requestLimits").Count > 0)
    {
        var attrMaxAllowedContentLength = xmlDocument.GetElementsByTagName("requestLimits")[0].Attributes.Cast<System.Xml.XmlAttribute>().FirstOrDefault(atributo => atributo.Name.Equals("maxAllowedContentLength"));
        return (Convert.ToDecimal(attrMaxAllowedContentLength.Value) / (decimal)(Math.Pow(1024, 2)));
    }

    //Default value of the configuration
    return (decimal)28.6;
}

但我认为这不是最好的解决方案。

P.S.:我正在处理 maxRequestLengthmaxAllowedContentLength 的值可能不同的可能性。

P.S.2.:我知道 Microsoft.Web.Administration,但我需要一个不涉及此 dll 的解决方案。

【问题讨论】:

标签: c# asp.net .net webforms web-config


【解决方案1】:

我的回答是你的最终结论是正确的。

我找不到更好的了。我对您的答案进行了一些调整,并包括将 [MaxRequestLength] 正确获取为字节的能力。然后,我对这两个值执行 Math.Min,让我的用户知道实际限制是这两个设置中的较低值。

    /// <summary>
    /// Get [maxAllowedContentLength] from web.config
    /// </summary>
    internal static long GetMaxAllowedContentLengthBytes()
    {
        const long DefaultAllowedContentLengthBytes = 30000000;
        using (System.IO.StreamReader reader = new System.IO.StreamReader(System.Web.HttpRuntime.AppDomainAppPath + "/web.config"))
        {
            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            xmlDocument.LoadXml(reader.ReadToEnd());

             if (xmlDocument.GetElementsByTagName("requestLimits").Count > 0)
            {
                var maxAllowedContentLength = xmlDocument.GetElementsByTagName("requestLimits")[0].Attributes.Cast<System.Xml.XmlAttribute>().FirstOrDefault(atributo => atributo.Name.Equals("maxAllowedContentLength"));
                return Convert.ToInt64(maxAllowedContentLength.Value);
            }
            else
                return DefaultAllowedContentLengthBytes;
        }
    }

    /// <summary>
    /// Get [MaxRequestLength] from web.config
    /// </summary>
    internal static long GetMaxRequestLengthBytes()
    {
        return (HttpContext.Current.GetSection("system.web/httpRuntime") as System.Web.Configuration.HttpRuntimeSection).MaxRequestLength * 1024;
    }

【讨论】:

  • 为什么访问 MaxRequestLength 比访问 maxAllowedContentLength 容易得多?
【解决方案2】:

该答案依赖于 web.config 中现有的设置。 您要么必须放入多个 If 语句来处理不存在的标签,要么处理抛出的错误。 我需要的是一种获取数字的方法,即使它是在 IIS 中而不是在 web.config 中设置的。 我使用了微软here 推荐的解决方案(见页面底部的代码)

这是我在 VB 中所做的

Private Function GetmaxAllowedContentLength() As Nullable(Of Int64)
    Dim serverManager As ServerManager = New ServerManager
    Dim config As Configuration = serverManager.GetWebConfiguration(GetExecutingAssembly.GetName.Name)
    Dim requestFilteringSection As ConfigurationSection = config.GetSection("system.webServer/security/requestFiltering")
    Dim requestLimitsElement As ConfigurationElement = requestFilteringSection.GetChildElement("requestLimits")
    Dim value As Nullable(Of Int64) = requestLimitsElement.Item("maxAllowedContentLength")

    Return value
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    相关资源
    最近更新 更多