QUIZ: What's Wrong With This Code?

0 comments suggest edit

This is a simplified version of a sneaky bug I ran into today (I’m fine thank you, but the bug is dead). The only prize I can offer is a GMail account if you want one.

Imagine that the method HandleRedirect actually does something interesting and if all the conditions pass, the user is redirected to special.aspx. This is the source code for an HttpHandler implemented as a .ashx file.

<%@ WebHandler Language="C#" Class="MyHandler" %>
using System;
using System.Web;
 
public class MyHandler : IHttpHandler
{
    /// <summary>
    /// Processs an incoming request.
    /// </summary>
    public void ProcessRequest(HttpContext ctx)
    {
        try
        {
            HandleRedirect(ctx);
        }
        catch(Exception)
        {
            ctx.Response.Redirect("/default.aspx");
        }
    }
 
    void HandleRedirect(HttpContext ctx)
    {
        ctx.Response.Redirect("/special.aspx");
    }
 
    public bool IsReusable
    {
        get { return true; }
    }
}
Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

7 responses

  1. Avatar for Daniel Turini
    Daniel Turini November 17th, 2004

    My guesses:

    1. The Class="SwitchHandler" is wrong, it should be MyHandler.

    2. Never, never, catch (Exception) and don't log it. Actually, avoid catch (Exception), unless your only purpose it to log it.

  2. Avatar for Brad Wilson
    Brad Wilson November 17th, 2004

    Yep, I've run into this, too.



    A hint: the way he's calling Response.Redirect, it will result in a call to Response.End.



    By the way, there's a related oddity in HTTP handler factories. Ever run across that one?

  3. Avatar for Haacked
    Haacked November 17th, 2004

    Daniel: 1. See what happens when you try to take a real-world problem and convert it into a sample. ;) That was a typo and not the intended "problem". But good catch. I fixed that. 2. Ok, that's a guideline that I violate in order to make this sample simple. However, there's a bigger problem with this code.



    Brad: Good hint!

  4. Avatar for BigJimSlade
    BigJimSlade November 17th, 2004

    Response.Redirect will alsway fire thread aborted exception i beleive there is an method overload that modifies this behavoir

  5. Avatar for Haacked
    Haacked November 17th, 2004

    That's it!

  6. Avatar for you've been HAACKED
    you've been HAACKED April 9th, 2007

    T-SQL Quiz - Dealing With Trends

  7. Avatar for Community Blogs
    Community Blogs April 9th, 2007

    I’m not one to post a lot of quizzes on my blog. Let’s face it, while we may create altruistic reasons