How to read system value from web.config and use in ASP.NET MVC C# method

后端 未结 3 1687
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 08:39

I\'m working on a ASP.NET MVC3 web application (not written by me) which has a max upload size of 100MB. Now this web application gets installed on server machines for customers

相关标签:
3条回答
  • 2021-02-02 09:01

    Try:

    var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/")
    var section = (System.Web.Configuration.SystemWebSectionGroup)config.GetSectionGroup("system.web")
    var maxRequestLength = section.HttpRuntime.MaxRequestLength
    
    0 讨论(0)
  • 2021-02-02 09:03

    There seems to be no easy way to read the system.webServer section, because it is marked as "ignored" from machine.config.

    One way is to parse the XML of the web.config file directly:

    var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
    var section = config.GetSection("system.webServer");
    var xml = section.SectionInformation.GetRawXml();
    var doc = XDocument.Parse(xml);
    var element = doc.Root.Element("security").Element("requestFiltering").Element("requestLimits");
    string value = element.Attribute("maxAllowedContentLength").Value;
    
    0 讨论(0)
  • 2021-02-02 09:06

    You can do something like:

    int maxRequestLength = 0;
    HttpRuntimeSection section =
    ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
    if (section != null) 
        maxRequestLength = section.MaxRequestLength;
    
    0 讨论(0)
提交回复
热议问题