title: How to read system value from web.config and use in ASP.NET MVC C# method
body: <p>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.</p>
  
  <p>Now there's a value in the web.config like so:</p>
  
  <pre><code>&lt;system.webServer&gt;
      &lt;security&gt;
          &lt;requestFiltering&gt;
              &lt;requestLimits maxAllowedContentLength="104857600" /&gt;
          &lt;/requestFiltering&gt;
      &lt;/security&gt;
  &lt;/system.webServer&gt;
  </code></pre>
  
  <p>Also there's another value here which appears to be similar:</p>
  
  <pre><code>&lt;system.web&gt;
      &lt;httpRuntime maxRequestLength="104857600" executionTimeout="360" /&gt;
  &lt;/system.web&gt;
  </code></pre>
  
  <p>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 <code>public const double MaxContentSize = 104857600</code> and another C# method was using that value to accept/deny the Ajax file upload.</p>
  
  <p>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.</p>
  
  <p>Can you do something like this?</p>
  
  <pre><code>MaxContentSize = ConfigurationManager.systemWeb.httpRuntime['maxRequestLength'];
  </code></pre>
  
  <p>I've seen some examples using appSettings in the web.config e.g.</p>
  
  <pre><code>&lt;appSettings&gt;&lt;add key="MySetting" value="104857600" /&gt;&lt;/appSettings&gt;
  </code></pre>
  
  <p>then accessing it like:</p>
  
  <pre><code>ConfigurationManager.AppSettings["MySetting"]
  </code></pre>
  
  <p>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?</p>
  
  <p>Many thanks</p>