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

3

34

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 so it would be nice if this value max upload size was configurable for each customer. They have access to edit the web.config for the web application if need be.

Now there's a value in the web.config like so:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="104857600" />
        </requestFiltering>
    </security>
</system.webServer>

Also there's another value here which appears to be similar:

<system.web>
    <httpRuntime maxRequestLength="104857600" executionTimeout="360" />
</system.web>

That 104857600 bytes appears to be the 100MB file upload limit. However on changing the value I discovered that this isn't the authoritative value and it wasn't obeying the new limit. So after some more digging I found somewhere else in the C# code was a hardcoded value public const double MaxContentSize = 104857600 and another C# method was using that value to accept/deny the Ajax file upload.

So what I think I'd like to do is replace that hard coded number in the code so it reads from the value in the web.config. Then at least anyone can change that value in the web.config when they deploy the website.

Can you do something like this?

MaxContentSize = ConfigurationManager.systemWeb.httpRuntime['maxRequestLength'];

I've seen some examples using appSettings in the web.config e.g.

<appSettings><add key="MySetting" value="104857600" /></appSettings>

then accessing it like:

ConfigurationManager.AppSettings["MySetting"]

But that would mean adding a custom value in there and now we'd have 3 places to change it in the web.config. Anyone got an idea how to do it properly?

Many thanks

Sector answered 9/1, 2013 at 23:45 Comment(0)
R
45

You can do something like:

int maxRequestLength = 0;
HttpRuntimeSection section =
ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
if (section != null) 
    maxRequestLength = section.MaxRequestLength;
Romain answered 10/1, 2013 at 1:24 Comment(1)
Thank-you this works! It requires two namespace includes: using System.Configuration; and using System.Web.Configuration;. I've accepted your answer as it has error handling if the httpRuntime section doesn't exist it falls back to a default value which is nice. I tried removing the whole <httpRuntime /> section from the Web.config and the file upload fails completely somewhere before it can get to this code, so I'm guessing it is definitely needed. So if I get rid of the default value code then that brings it down to 2 lines, nice.Sector
F
12

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;
Flashlight answered 10/1, 2013 at 1:49 Comment(3)
Thank-you! This will be useful if I need to access the system.webServer section in future. I managed to get it working with just the MaxRequestLength attribute instead. The maxAllowedContentLength in system.webServer/security/requestFiltering/requestsLimits seems to be overriden by the MaxRequestLength attribute in system.web/httpRuntime.Sector
They don't seem to be overridden in my testing. I have to set both values.Fertile
+1 for the usefulness of the example for people like me who come looking for a way to read the system.webserver section.Kooima
U
7

Try:

var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/")
var section = (System.Web.Configuration.SystemWebSectionGroup)config.GetSectionGroup("system.web")
var maxRequestLength = section.HttpRuntime.MaxRequestLength
Undress answered 10/1, 2013 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.