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

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

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

  • ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) DiskJunky 建议,但它会抛出 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 的解决方案。

最佳答案

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

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

关于c# - 如何以编程方式从 web.config 的属性 "maxAllowedContentLength"中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46545559/

相关文章:

c# - 获取数组索引的优雅方式

在 Web 部署项目期间,ASP.NET 编译器失败并出现 XmlSerializers 错误

c# - 单击 CheckBoxList 上的项目时需要显示一些内容

c# - 如何在 Linux 的单声道解决方案中删除对 System.Deployment 的引用?

.net - 这是 F# 结构化 printf 错误吗?

.net - 下次如何加载动态创建的设置属性?

c# - 重写正则表达式以删除 c#6 依赖项

c# - 等效于 WPF .NET 5、.NET 6 或 .Net Core 中的 UserSettings/ApplicationSettings

c# - 为什么 TextRenderer 在测量文本时添加边距?

c# - 使用特定数据跟踪数据库插入