How to read maxAllowedContentLength

For my web application, I have a Flash component for downloading files. I would like to handle the client-side maximum file size limit without actually sending this file to the server. Therefore, I need to somehow read this value from the configuration file in order to send it to the client. Some articles I found say that reading the configuration file directly is not a solution, because it can be changed in many places. So there must surely be some kind of API call, but I can't find ...

<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="1048576" /> </requestFiltering> </security> </system.webServer> 
+4
source share
3 answers

Try this way

You can change the lower code segment based on the web configuration in the configuration file

My web.config looks like

 <system.web> <httpRuntime executionTimeout="30" maxRequestLength="100"/> 

Here you can see that maxRequestLength is defined as 100, which can be changed from code by page

Add using System.Web.Configuration;

Now write this code to change the value of maxRequestLength

 Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~"); HttpRuntimeSection httpruntinesec = (HttpRuntimeSection)configuration.GetSection("system.web/httpRuntime"); 

You can use an instance of httpruntinesce to set the value.

0
source

I know this is an old question, but it cost me so much time (wasted) that I wanted to publish a working solution for those who might come my way:

 Using Microsoft.Web.Administration; uint uiMaxAllowedContentLength = 0; using (ServerManager serverManager = new ServerManager()) { Configuration config = serverManager.GetWebConfiguration("Default Web Site/{{your special site}}"); ConfigurationSection requestFilteringSection = config.GetSection("system.webServer/security/requestFiltering"); ConfigurationElement requestLimitsElement = requestFilteringSection.GetChildElement("requestLimits"); object maxAllowedContentLength = requestLimitsElement.GetAttributeValue("maxAllowedContentLength"); if (null != maxAllowedContentLength) { uint.TryParse(maxAllowedContentLength.ToString(), out uiMaxAllowedContentLength); } } 

Make sure you first download and install the Microsoft web administration package (

PM> Microsoft.Web.Administration Installation Package

)

In addition, you may need to configure permission for your web.config file. Give IUSR and IIS_IUSRS at least Read permission.

The code is actually from the Microsoft site, although finding it forever! I hope I saved you a couple of hours.

Greetings

Roman

+6
source

Without Microsoft Web Administration Pack:

 using System.Web.Configuration; using System.Configuration; using System.Xml; using System.IO; Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~"); IgnoreSection ignoreSection = configuration.GetSection("system.webServer") as IgnoreSection; string sectionXml = ignoreSection.SectionInformation.GetRawXml(); StringReader stringReader = new StringReader(sectionXml); XmlTextReader xmlTextReader = new XmlTextReader(stringReader); UInt32 maxAllowedContentLength = 0; if(xmlTextReader.ReadToDescendant("requestLimits")) UInt32.TryParse(xmlTextReader.GetAttribute("maxAllowedContentLength"), out maxAllowedContentLength); 
0
source

Source: https://habr.com/ru/post/1385501/


All Articles