我需要从 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.:我正在研究 maxRequestLength
和 maxAllowedContentLength
的值可能不同的可能性。
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/