Interesting QueryString parameter behavior in ASP.NET

I recently ran into some strange behavior in ASP.NET when browsing to the error-log of my current project. Somebody was passing in some invalid query string parameters. That shouldn’t be a problem, but for some reason the error log contained some null-reference exceptions.

The query string was something along the lines of ?a=b&c&d=e. Notice the ‘c’ parameter isn’t in key=value format.

The culprit was the following piece of code:


var keysWithStuff = Request.QueryString.AllKeys.Where(x => x.Contains("stuff"));

but why?

Well, upon debugging I learned ASP.NET interprets the query string the following way:

Request.QueryString
{a=b&c&d=e}
    [System.Web.HttpValueCollection]: {a=b&c&d=e}
    base {System.Collections.Specialized.NameObjectCollectionBase}: {a=b&c&d=e}
    _all: null
    _allKeys: {string[3]}
    AllKeys: {string[3]}

Request.QueryString.AllKeys
{string[3]}
    [0]: "b"
    [1]: null
    [2]: "e"

Request.QueryString[1]
"c"

This came as a surprise because I distinctly remembered those query-string parameters being treated as keys in PHP; so I more or less assumed that’s how they were supposed to work.

I double-checked with a quick PHP script:


print_r($_REQUEST);

and indeed:

Array
(
    [a] => b
    [c] => 
    [d] => e
)

So, keep that in mind when using empty query string params.

Posted in .NET | Tagged , | Leave a comment

Making pretty database exports of HTML content

Not really .NET-related, but I thought I’d share it anyway:
Recently one of the account managers at my office asked for an Excel-sheet of all the e-mail templates for one of our application. He had already gotten one from a colleague, but it was just a straight dump from the query-result window of SQL Management Studio. It contained lots of HTML-tags and wasn’t readable for non-developers.

So, determined to spend more than 2 minutes and give him something useful, I took the following steps:
1. Fire up SQL Management Studio (in my case, the content was stored in an SQL Server database; use a management IDE of your chosing).
2. Run a SELECT-query of all the fields you want in your export. (note: by default the query-result window will only contain a limited amount of characters per cell; make sure you up that amount in the configuration-screen).
3. Copy/paste the query result to a new text file. All the cells should be TAB-separated.
4. Find/replace TAB with ‘</td><td>’.
5. Place a ‘<tr><td>’ at the beginning and a ‘</td></tr>’ at the end of each line*.
6. Surround it all with ‘<html><body><table>’ and ‘</table></body></html>’.
7. Open your file in Internet Explorer (other browsers don’t seem to copy the formatting the way IE does which would give problems in the following step).
8. Open Excel and paste the content from IE.
9. Voila, you now have an export of your database content which is manager- and customer-friendly.

*: In my case the cell-content didn’t contain newlines. If yours does, you might want to remove those or replace them with ‘<br />’ tags, depending on your needs.

Posted in Uncategorized | Leave a comment