17

In the past I have used some code by I think Scott Hanselman of Microsoft. However now I am using MVC5 and I don't think that code is valid any more.

Is there a way I can trace routes taken in MVC5 so that I can know why I see messages like:

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

2 Answers2

19

I know it is late for the OP but for anyone else trying to debug 404 errors I've found a way to intercept the route result and see why it is failing to find the resource.

In Global.asax.cs override Init like this:

    public override void Init()
    {
        base.Init();
        this.AcquireRequestState += showRouteValues;
    }

    protected void showRouteValues(object sender, EventArgs e)
    {
        var context = HttpContext.Current;
        if (context == null)
            return;
        var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(context));
    }

The routeData variable will hold the route info as it is being interpreted. I've tested this on MVC5.

I originally bumped into this method in another answer by Paul Evans, this is the link (thanks to @porcus for finding it): stackoverflow.com/a/25466524

Community
  • 1
  • 1
c0y0teX
  • 1,462
  • 1
  • 23
  • 25
  • 2
    Here's the link to the other SO answer you may have gotten that code snippet from: http://stackoverflow.com/a/25466524 And I commend you for trying to provide attribution. – porcus Mar 12 '16 at 00:23
  • @porcus that is the original answer indeed, I'll update my answer to link to it, thanks. – c0y0teX Mar 23 '16 at 16:40
3

Take a look at Glimpse. One of the modules that it comes with is a Routes module which will allow you to see details about the routes that were checked, values that were passed in and which ones matched (if any).

Andy T
  • 10,223
  • 5
  • 53
  • 95