0

This SO answer presents an elegant solution to handling 404 errors. I modified the code given in that answer to also handle 400 errors (the response that happens when someone feeds a "potentially dangerous" character my website). My code looks like this:

/// <summary> Handle "page not found" (HTTP 404) and "dangerous/invalid syntax" (HTTP 400) errors. </summary>
protected void Application_EndRequest()
{
    // This code is adapted from: https://stackoverflow.com/a/9026941/1637105
    var code = Context.Response.StatusCode;
    if (code == 404 || code == 400) {
        Response.Clear();

        var rd = new RouteData();
        rd.DataTokens["area"] = "AreaName"; // In case controller is in another area
        rd.Values["controller"] = "Errors";
        rd.Values["action"] = "NotFound";

        IController c = new ErrorsController();
        c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
    }
}

My problem is that if I have glimpse installed then, when I get a 400 error (for example, if I navigate to http://example.com/HiBob:), I get:

An exception of type 'System.NullReferenceException' occurred in Glimpse.Core.dll

Since glimpse doesn't seem to have any problem with the 404 errors that get rerouted through this code, I assume that glimpse is choking on the request path.

Is there something I can do in this code to URL-encode the request path before I send the new request off to be executed by the MVC pipeline?

EDIT: I updated the glimpse NuGet package to the latest version and the problem seems to be gone. Life is good. I'm an idiot (for not making sure I have the latest stuff before complaining that it doesn't work).

Community
  • 1
  • 1
Bob.at.Indigo.Health
  • 11,023
  • 13
  • 64
  • 111
  • Could you show the complete stacktrace? What version of Glimpse are you using? – cgijbels Jul 14 '14 at 06:53
  • @cgijbels Yeah, about that... I was using a seriously old version of glimpse. I updated to the latest version and the problem seems to be gone now. (I've been afraid to update my NuGet packages because Microsoft has introduced some seriously breaking changes in their updates, and some other packages have references to the MS stuff that might try to give me those breaking changes. I'm working on a completely new incarnation of the project, starting with fresh VS 2013 projects. LOTS of work changing to new account model, async methods, Bootstrap views...) – Bob.at.Indigo.Health Jul 14 '14 at 23:13

0 Answers0