如何以编程方式从 web.config 的属性 "maxAllowedContentLength" 获取值?

How to get the value from attribute "maxAllowedContentLength" of web.config programmatically?

我需要从 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

我已经试过了:

此外,我编写了以下代码:

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 的解决方案。

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

我找不到更好的了。我通过一些调整结束了您的回答,并包含了将 [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;
    }

该答案依赖于 web.config 中存在的设置。 您要么必须放入多个 If 语句来处理不存在的标签,要么处理抛出的错误。 我需要的是一种获取数字的方法,即使它是在 IIS 中而不是在 web.config 中设置的。 我使用了 Microsoft 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