links for 2009-12-22

Posted in Daily Links by Sumit Thomas on December 22, 2009

Handling Exceptions using jQuery and ASP.NET MVC

Posted in AJAX, ASPNETMVC, jquery by Sumit Thomas on December 22, 2009

[tweetmeme style=”compact”] Using AJAX has become one of the de facto practices in many web based RIAs. The use of jQuery is on the rise in many web applications, especially the ones built using ASP.NET MVC. Making AJAX calls using jQuery is quick and easy. A typical web request can have an expected response or an exception. It is therefore important to catch exceptions when we make any AJAX calls so that we can show some message which makes sense to the user.

Lets begin with our Controller. Create the following method to throw JSON version of the exception.

        private JsonResult ThrowJSONError(Exception e)
            Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
            //Log your exception
            return Json(new { Message = e.Message});

Specifying the StatusCode as HttpStatusCode.BadRequest or HttpStatusCode.InternalServerError or simply 500 will let the javascript know that there is something wrong in the response.

Lets create an ActionMethod to throw an exception which we will catch using jQuery.

        public ActionResult DivideByZero()
                throw new DivideByZeroException();
            catch (DivideByZeroException e)
                return ThrowJSONError(e);


The above method will throw a DivideByZeroException which will be converted to a JSON string in the catch block and returned to the caller. Lets write the jQuery code to call the ActionMethod.

        $(document).ready(function() {
                type: "GET",
                url: "AJAX/DivideByZero",
                dataType: "json",
                success: function(data) {
                    if (data) {
                }, error: function(xhr, status, error) {

        function DisplayError(xhr) {
            var msg = JSON.parse(xhr.responseText);

Here we are making a call to the DivideByZero ActionMethod. Obviously the exception will be thrown,which will be caught in the error block. The responseText is a string of serialized data, which will not be useful unless we parse it to JSON. We can use the native JSON parser available in the latest browser. But, this as I found out is not helpful in IE 7 or lower. Fortunately, there is a useful library at JSON.org that can parse the responseText to JSON.

Let me know if there are better ways of handling exceptions in jQuery.

Hope you found it useful!

Tagged with: , ,

YouTube says I have shorter URLs too!

Posted in technology by Sumit Thomas on December 22, 2009

[tweetmeme style=”compact”]Following Google’s launch of its Short URL service Goo.gl, its subsidiary Youtube has come out with its own Short URL service Youtu.be

YouTube’s intention for coming up with its own Short URL service is as per this blog to provide a shorter version of Youtube links.

To use youtu.be manually, simply take a URL like http://www.youtube.com/watch?v=FdeioVndUhs and replace the “http://www.youtube.com/watch?v=” with “http://youtu.be/” to get: http://youtu.be/FdeioVndUhs Plug that shorter URL into a browser, and you’ll see it redirects to that video.

Well it doesn’t look too short to me. The shorter version of http://www.youtube.com/watch?v=FdeioVndUhs in bit.ly is http://bit.ly/7Se5hu compared to Youtu.be’s http://youtu.be/FdeioVndUhs. The Video ID in YouTube links are very long and needs to be shortened further.

The big question for me though is why did YouTube come up with its own Shorter URLs when they could have used Goo.gl? Is it some kind of marketing technique or is it YouTube’s way of saying we have it too!

Whats next? Yah.oo?