I need to access the system.webServer/security/requestFiltering/requestLimits section from the web.config file, to get the value of the attribute maxAllowedContentLength. This value is needed to validate a configuration, so the user can't set a higher value than the value defined inside the web.config file. To validate this configuration, the value from the attribute maxRequestLength (system.web/httpRuntime) is also needed, but we are already getting that value by the code below:
(ConfigurationManager.GetSection("system.web/httpRuntime") as System.Web.Configuration.HttpRuntimeSection).MaxRequestLength
I've already tried:
(ConfigurationManager.GetSection("system.webServer") as IgnoreSection).SectionInformation.GetRawXml(), but it throws aSystem.InvalidOperationException.(System.Web.Configuration.WebConfigurationManager.GetSection("system.webServer") as IgnoreSection).SectionInformation.GetRawXml(), but it also throws aSystem.InvalidOperationException.ConfigurationManager.GetSection("system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength"), but it returnsnull.System.Web.Configuration.WebConfigurationManager.GetSection("system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength"), but it also returnsnull.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)as suggested by DiskJunky, but it throws aSystem.ArgumentException, with the message "exePath must be specified when not running inside a stand alone exe".
Also, I made the code below:
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;
}
But I tought it was not the best solution.
P.S.: I'm working with the possibility that the value from maxRequestLength and maxAllowedContentLength may differ.
P.S.2.: I know about the Microsoft.Web.Administration, but I need a solution that doesn't involve this dll.