Is there a way I can debug a route in ASP. MVC5? [duplicate]

独自空忆成欢 提交于 2019-12-03 23:35:40

问题


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.

回答1:


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




回答2:


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).



来源:https://stackoverflow.com/questions/19058463/is-there-a-way-i-can-debug-a-route-in-asp-mvc5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!