Retreiving maxRequestLength Value From Web.Config

I was trying to modify a .NET application so that I don’t have to make a bunch of code changes every time the maxRequestLength value in the web.config is changed. I like to have things be as automated as possible. Being able to simply update the value in the web.config file and have your application updates itself has a lot of benefits, such as reducing time needed to make changes, reducing the possibility of introducing bugs and ensuring all areas get updated.

However, when I started researching how to access this section of the web.config file, I kept seeing all over the Internet that you can not access this configuration section. I thought that if there wasn’t a specific method available that I could simply access it via the ConfigurationSettings.AppSettings[“someKey”] functionality, but I heard that this section wasn’t accessible via that method either. Everything I kept seeing was that you had to do this manually.

So, that’s what I did for a while and simply pulled it all from a single variable that I set at the beginning of the code. However, today there was a request for another increase in the request limit. So, I looked into this issue again, but this time directly from Microsoft’s site (from the horse’s mouth so to speak). I found this documentation that shows how to access this section on the MSDN site, which says to do this.

int_MaxRequestLength = configSection.MaxRequestLength;

It wasn’t entirely clear what the configSection object was instantiated from, so I did a search on HttpRuntimeSection.MaxRequestLength and found this post showing specifically how to implement this functionality. It shows to call this.

HttpRuntimeSection section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
int_MaxRequestLength = section.MaxRequestLength;

I implemented this in code like this.

// Set the maximum file size for uploads in bytes.
HttpRuntimeSection section = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
if (section != null)
    // Retreive the maximum request length from the web.config and convert to bytes.
    int_FileSizeLimit = (section.MaxRequestLength * 1024);
    int_FileSizeLimit = 31457280;

It worked great! Now all I have to do is change the maxRequestLength property in the web.config file and all of the messages, error messages, file limits, etc. change automatically! It doesn’t require recompiling the site and publishing every time a change is made and it eliminates the possibility of me forgetting to update something or introducing spelling mistakes and the like.


8 thoughts on “Retreiving maxRequestLength Value From Web.Config

  1. Thank you so much.. it worked and helped me a lot. could be more useful if i could do this in javascript. thank you that this worked too….

    1. Daniel,

      I’m very glad it helped you! I’m not a JavaScript genius, but there are security considerations when you start trying to access files on disc from JavaScript in a browser. Also, JavaScript can be disabled on a browser by browser basis, so it would work for some clients, but not others. It’s just not the cleanest solution.

      Since this way is all within .NET, there’s an existing API to make life easier for you. Also, since the .NET engine is performing the work and the web.config is read by the .NET engine, there’s not really any explicite file access going on, so you avoid the browser conspiring against you in the name of security.

      Anyway, thanks again! Please keep up the comments and check out my other articles.

  2. Thanks for one’s marvelous posting! I truly enjoyed reading it, you happen to be a great author.I will remember to bookmark your blog and will often come back later on. I want to encourage you to continue your great job, have a nice holiday weekend!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s