简体   繁体   English

调用ASP.NET MVC控制器/视图

[英]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? 在MVC中,是否可以确定在单个响应中调用所有视图和/或控制器的方法?

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. 我认为框架不会直接公开此内容,但是您可以使用OnActionExecuted方法并记录每个调用的操作以及唯一的请求标识符。 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). 您可以连接到global.asax.cs中的Application_BeginRequest来生成一个GUID用作该请求的唯一ID(存储在Session中,但对于每个新请求均被覆盖)。 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. 如果使用基本控制器并从中派生所有控制器,则可以将日志记录放入基本控制器的OnActionExecuted方法中以使其保持DRY状态。

Alternatively, you could look at creating a custom ActionInvoker and put the logging there. 或者,您可以查看创建自定义ActionInvoker并将日志记录放在此处。

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. 当客户端收到渲染视图时,任何在客户端启动的AJAX请求都将显示为不同的请求。 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. 如果您还需要包括这些ID,则唯一的ID生成代码应该只应在非AJAX请求上运行,否则将现有ID保留在会话中。 Typically AJAX requests have an HTTP_X_Requested_With header to distinguish them. 通常,AJAX请求具有HTTP_X_Requested_With标头来区分它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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