Archive for May, 2009

Avoid Caching of Ajax Requests

So we recently experienced a subtle little bug in an ajax enabled asp.net mvc application that only showed up in IE7.  Turns out that the browser was caching an ajax request.  To get around it, we made the following addition to the global.asax http application class:

public abstract class MyWebApplication : HttpApplication
{
    protected MyWebApplication()
    {
        this.BeginRequest += new EventHandler(MyWebApplication_BeginRequest);
    }

    void MyWebApplication_BeginRequest(object sender, EventArgs e)
    {
        string requestedWith = this.Request.Headers["x-requested-with"];
        if (!string.IsNullOrEmpty(requestedWith) && requestedWith.Equals(“XMLHttpRequest”, StringComparison.InvariantCultureIgnoreCase))
        {
            this.Response.Expires = 0;
            this.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            this.Response.AddHeader(“pragma”, “no-cache”);
            this.Response.AddHeader(“cache-control”, “private”);
            this.Response.CacheControl = “no-cache”;
        }
    }
}

Basically, any request that comes in with the x-requested-with header (which most or all ajax libs use) will get these extra headers added to the response to tell the browser to avoid caching.

Hope it helps someone else out there :-)

Comments (4)

VSClean Command Line Tool

I don’t know about you, but I have a lot of code on my computer.  As anyone who follows me on Twitter knows, I’m often running out of space on my laptop.  So yesterday, I was running WinDirStat on my harddrive to see what I could clean up.  I found that I had a significant amount of space spent on the contents of my /bin/* folders.  As any developer would know, this is where Visual Studio places binary files when it compiles.  So any time I have a visual studio solution from some old project, inevitably, I’ll have tons and tons dlls and exes just sitting around, not to mention content that may be part of the solution such as .mdb files, and in the more extreme cases, XNA game content.

So I started opening the folders and deleting them when I lamented that I wished I could just tell visual studio to run a Clean Solution command on all these files.  Then it hit me, visual studio uses MSBuild to do it’s compiling.  So why not write a simple tool that recurses through my dev directory and runs the msbuild clean command on all the solutions it finds.  Brilliant!

To that end, I’d like to introduce the command line tool VSClean.
Download here: http://codecube.net/bloguploads/VSClean.zip

Update: This project has been moved to CodePlex: http://vsclean.codeplex.com/

The zip file above is the source code.  Once you compile it, just run it from the command line and pass in your root source code directory as the first parameter:

> VSClean “c:\dev”

Once you run this, it’ll iterate through all the folders in that directory looking for .sln files.  Once it finds them, it runs this command:

C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe “c:\dev\theSolution.sln” /m /t:clean

Of course, if you’ve installed the framework in a different path, you’ll have to change the const variable at the top of the class.  Once this is done, you can feel good that all of your old source code isn’t taking up unnecessary space :-)

Comments (2)

Bayesian Filtering with C#

Introducing nBayes, a new open source projectnbayes_logo which can be found here:

http://nbayes.codeplex.com/

nBayes is a simple implementation of the naive bayesian spam filter described by Paul Graham in his essay "A Plan for Spam" (http://www.paulgraham.com/spam.html). The API is very simple, there are just 3 classes that you need to be familiar with.

You can train the Index by adding entries to it, and then use an Analyer to categorize a new entry as belonging to one index or another. In the spam filtering example, one index would be the Spam, while the other would be the "not-Spam".

Sample Code

Index spam = Index.CreateMemoryIndex();
Index notspam = Index.CreateMemoryIndex();

// train the indexes
spam.Add(Entry.FromString("want some viagra?"));
spam.Add(Entry.FromString("cialis can make you larger"));
notspam.Add(Entry.FromString("Hello, how are you?"));
notspam.Add(Entry.FromString("Did you go to the park today?"));

Analyzer analyzer = new Analyzer();
CategorizationResult result = analyzer.Categorize(
     Entry.FromString("cialis viagra"),
     spam,
     notspam);

switch (result)
{
    case CategorizationResult.First:
        Console.WriteLine("Spam");
        break;
    case CategorizationResult.Undetermined:
        Console.WriteLine("Undecided");
        break;
    case CategorizationResult.Second:
        Console.WriteLine("Not Spam");
        break;
}

The example above uses an extremely small index of words … however, the reported result is indeed that it categorizes it as spam. Larger indexes are required to get better results. The sample project provided in the source code shows how to create two indexes by doing a search of twitter for two different terms. The top 100 results of that twitter API query will be trained into each respective index, and then it will ask you to type in a sample phrase. This phrase will be categorized into one of each index.

Comments (1)

Computing for Children

I’ve mentioned to a few people that I recently set my kids up with PCs.  I think back to my first interactions with a computer when I was very young.  My parents bought an Acer IBM Clone from Fingerhut.  I learned so much from using that machine … in an effort to play prince of persia at school, my brother taught me how to make a boot disk to boot the library computer’s into DOS, then launch the game from the command line.  It really taught me a lot about the basics of how the computer works … knowledge that I know has been useful in my career.

So I wanted them to have that same opportunity.  I wanted them to be able to play games, and access learning tools on the internet; but wanted them to do it in as safe an environment as I could manage. So to that end, here’s their setup:

I did quite a bit of research before running out and getting the kids the netbooks.  Although they were relatively cheap in the grand scheme of things, I didn’t want to spend $600 bucks (for two of them) if it wasn’t going to be safe.  I made sure that their user account was *not* an administrator, and the use of Firefox with adblock gave me a pretty good feeling that they’d be safe from exploits. 

My choice of Safe Eyes was done after reading a lot of reviews.  One of my favorite features was that you could log in and manage the site whitelist from any browser … so if there was some site they needed that was getting blocked, I could just unblock it from work after a phone call from them.  Also, safe eyes can do partial filtering of sites like youtube and google search pages so they can still use the sites, without inadvertently running into content I didn’t want them seeing.

Another little trick I set up was that I made network shortcuts to each other’s laptop and taught them how to move files back and forth.  Unfortunately, this wasn’t very easy … it involved having to manually set some permissions via the command line.  I wish I would have written the steps down so I could share, but alas.  At least they can do some simple file sharing.  I figure that as long as I log on with my admin account periodically and do the windows updates, that they should remain safe.

Of course, I know that nothing is foolproof … we also employ other counter-measures such as making sure to monitor their use.  This ensures that even if there was a way around the parental controls, that there was a physical deterrent, and also it ensured that my investment is secured (ie, they don’t break them).

Wow … It literally *just* occurred to me that both my first computer, and my kid’s first computers were Acers. Go figure :-)

Comments