This question already has an answer here:
这个问题在这里已有答案:
- How do you debug MVC 4 API routes? 5 answers
- 你如何调试MVC 4 API路由? 5个答案
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.
在过去,我认为微软的Scott Hanselman使用了一些代码。但是现在我正在使用MVC5,我认为代码不再有效。
Is there a way I can trace routes taken in MVC5 so that I can know why I see messages like:
有没有办法可以跟踪MVC5中的路由,以便我知道为什么我会看到如下消息:
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
2 个解决方案
#1
13
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.
我知道OP已经晚了但是对于其他试图调试404错误的人我发现了一种拦截路由结果的方法,看看为什么它找不到资源。
In Global.asax.cs
override Init like this:
在Global.asax.cs中覆盖Init,如下所示:
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.
routeData变量将在解释时保存路由信息。我在MVC5上测试了这个。
I originally bumped into this method in another answer by Paul Evans, this is the link (thanks to @porcus for finding it): *.com/a/25466524
我最初在Paul Evans的另一个答案中碰到了这个方法,这是链接(感谢@porcus找到它):*.com/a/25466524
#2
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).
看看Glimpse。它带来的模块之一是路由模块,它允许您查看有关已检查路由的详细信息,传入的值以及匹配的路由(如果有)。
#1
13
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.
我知道OP已经晚了但是对于其他试图调试404错误的人我发现了一种拦截路由结果的方法,看看为什么它找不到资源。
In Global.asax.cs
override Init like this:
在Global.asax.cs中覆盖Init,如下所示:
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.
routeData变量将在解释时保存路由信息。我在MVC5上测试了这个。
I originally bumped into this method in another answer by Paul Evans, this is the link (thanks to @porcus for finding it): *.com/a/25466524
我最初在Paul Evans的另一个答案中碰到了这个方法,这是链接(感谢@porcus找到它):*.com/a/25466524
#2
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).
看看Glimpse。它带来的模块之一是路由模块,它允许您查看有关已检查路由的详细信息,传入的值以及匹配的路由(如果有)。