问题
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