属性maxAllowedContentLength
の値を取得するには、web.configファイルからsystem.webServer/security/requestFiltering/requestLimits
セクションにアクセスする必要があります。この値は設定を検証するために必要な値なので、ユーザはweb.configファイル内で定義された値より高い値を設定できません。この構成を検証するには、属性maxRequestLength
(system.web/httpRuntime
)からの値も必要とされているが、我々はすでに以下のコードでその値を取得している:私はすでに試したweb.configの属性 "maxAllowedContentLength"からプログラムで値を取得するにはどうすればよいですか?
(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
を返します。
DiskJunkyによって提案されたが、それは「スタンドアローンexeファイルの内部で実行していないときに指定する必要がありますexePath」というメッセージと、System.Web.Configuration.WebConfigurationManager.GetSection("system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength")
ですが、null
も返します。System.ArgumentException
をスローするようConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
。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 .:私はmaxRequestLength
とmaxAllowedContentLength
の値が異なる可能性があると考えています。
P.S.2:私はMicrosoft.Web.Administrationについて知っていますが、私はこのdllを含まないソリューションが必要です。
可能重複https://stackoverflow.com/questions/34093968/get-maxrequestlength-value-from-specific-location-path-in-config ) – DiskJunky