Mat*_*aum 5 .net c# asp.net webforms web-config
我需要访问system.webServer/security/requestFiltering/requestLimitsweb.config 文件中的部分,以获取 attribute 的值maxAllowedContentLength。验证配置需要此值,因此用户无法设置比 web.config 文件中定义的值更高的值。maxRequestLength为了验证此配置,还需要属性( )中的值system.web/httpRuntime,但我们已经通过以下代码获取了该值:
(ConfigurationManager.GetSection("system.web/httpRuntime") as System.Web.Configuration.HttpRuntimeSection).MaxRequestLength
Run Code Online (Sandbox Code Playgroud)
我已经尝试过:
(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;
}
Run Code Online (Sandbox Code Playgroud)
但我认为这不是最好的解决方案。
maxRequestLengthPS:我正在研究和的值可能不同的可能性maxAllowedContentLength。
PS2.:我了解 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2323 次 |
| 最近记录: |