简体   繁体   中英

ASP.NET MVC Controllers/Views invoked

When I invoke a controller, it's possible that the view response calls other controllers. Is there a way to determine, in MVC, all of the views and/or controllers were called in a single response?

Thanks.

I don't think that the framework exposes this directly, but you could hook into the OnActionExecuted method and log each action that gets invoked along with a unique request identifier. You could hook into Application_BeginRequest in global.asax.cs to generate a GUID to use as the unique id for that request (stored in the Session, but overwritten for each new request). If you use a base controller and derive all your controllers from it, you could put the logging in the base controllers OnActionExecuted method to keep it DRY.

Alternatively, you could look at creating a custom ActionInvoker and put the logging there.

This is where I would start anyway, though, there might be a better way.

NOTE: This will only tie together actions that are invoked server-side for the request. Any AJAX requests kicked off client-side when client receives the rendered view will show up as different requests. If you need to include those as well, your unique id generation code should probably only run on non-AJAX requests, leaving the existing id alone in the session otherwise. Typically AJAX requests have an HTTP_X_Requested_With header to distinguish them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM