Thursday, October 22, 2009

Client-side Spell Checking with Asp.Net and jQuery

Overview

I couldn't find a client-side spell check that worked with Asp.Net, so I decided to use Brandon Aaron's jQuery plugin, "Spell Check". He gives you an PHP file to handle the request to the Google API, but I don't use PHP on my server, so I had to create the following Asp.Net Generic Handler.

Create a Generic Handler


Copy and paste this into the Generic Handler you just created:
string text, body, lang;

lang = context.Request.QueryString["lang"];

text = HttpUtility.UrlDecode(context.Request.QueryString["text"]);

body = ""
  + ""
   + "" + text + ""
  + "";


WebClient client = new WebClient();
client.Headers.Add("Content-Type", "text/xml");

byte[] buffer = Encoding.UTF8.GetBytes(body);

byte[] response = client.UploadData("https://www.google.com/tbproxy/spell?lang=" + lang, "POST", buffer);

string result = Encoding.UTF8.GetString(response);

context.Response.ContentType = "text/xml";
context.Response.Write(result);

Change the URL

Need to change the url in the jquery.spellcheck.js file (line 44 in Version 0.2-pre):