2017-10-03 119 views
1

属性maxAllowedContentLengthの値を取得するには、web.configファイルからsystem.webServer/security/requestFiltering/requestLimitsセクションにアクセスする必要があります。この値は設定を検証するために必要な値なので、ユーザはweb.configファイル内で定義された値より高い値を設定できません。この構成を検証するには、属性maxRequestLengthsystem.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を返します。

  • System.Web.Configuration.WebConfigurationManager.GetSection("system.webServer/security/requestFiltering/requestLimits/maxAllowedContentLength")ですが、nullも返します。

    DiskJunkyによって提案されたが、それは「スタンドアローンexeファイルの内部で実行していないときに指定する必要がありますexePath」というメッセージと、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 .:私はmaxRequestLengthmaxAllowedContentLengthの値が異なる可能性があると考えています。

P.S.2:私はMicrosoft.Web.Administrationについて知っていますが、私はこのdllを含まないソリューションが必要です。

+1

可能重複https://stackoverflow.com/questions/34093968/get-maxrequestlength-value-from-specific-location-path-in-config ) – DiskJunky

答えて

1

私の答えはあなたの最終的な結論が正しいことです。

私はそれ以上のものを見つけることはできませんでした。私はいくつかの調整を加えてあなたの答えをまとめ、正確に[MaxRequestLength]をバイトとして得る機能を含んでいました。次に、Math.Minを実行して、ユーザーに実際の制限が2つの設定のうち低い方であることを知らせるようにします。

/// <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; 
    } 
([設定の特定のロケーションパスから取得maxRequestLength値]の
関連する問題