Reputation: 48066
I recently had the misfortune to need to find the root cause of an error in an ASP.NET MVC action. The error turned out to be in one of the action filters declared on the action. However, finding this error was mostly sheer luck, and took more time than is reasonable. In the future, how can I debug problems with ASP.NET MVC action filters?
In particular:
I tried using the VS.NET debugger; however it won't step into the filters in a useful way. If I pause execution before the request the server waits for the debugger as appropriate, but when I then use Step into or step over, it just continues without stepping through any filters (just my code is off). I could probably set a breakpoint if I knew beforehand which filters were registered, but that's hardly practical especially since some filters are in third-party code.
Upvotes: 3
Views: 4432
Reputation: 16195
Following code will write the name of controllers and actions in the sequence they are called. I hope it will help someone else also as it helped me a lot.
It will be great if you have a base controller and have rest of the controllers inherit from it then following code will let you know all the controllers and actions called for any request.
#if DEBUG
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
string controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
string action = filterContext.ActionDescriptor.ActionName;
Debug.WriteLine("Controller-" + controller + ", Action-" + action);
}
#endif
P.S. - If you want more details like processing times of actions or some pluggable system then please use Glimpse. It takes only few minutes to get it running. It is a great tool. On a side note, the timings provided by glimpse are not accurate as there is some overhead of Glimpse is also included.
Upvotes: 6