调用 ASP.NET MVC 控制器/视图
当我调用控制器时,视图响应可能会调用其他控制器。在 MVC 中,有没有办法确定所有视图和/或控制器都在单个响应中调用?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为框架不会直接公开这一点,但您可以挂钩 OnActionExecuted 方法并记录调用的每个操作以及唯一的请求标识符。您可以挂钩到 global.asax.cs 中的 Application_BeginRequest 来生成一个 GUID,用作该请求的唯一 ID(存储在会话中,但会为每个新请求覆盖)。如果您使用基本控制器并从中派生所有控制器,则可以将日志记录放在基本控制器的 OnActionExecuted 方法中以保持干燥。
或者,您可以考虑创建一个自定义 ActionInvoker 并将日志记录放在那里。
无论如何,这就是我要开始的地方,不过,可能有更好的方法。
注意:这只会将服务器端为请求调用的操作结合在一起。当客户端收到渲染视图时,客户端启动的任何 AJAX 请求都将显示为不同的请求。如果您还需要包含这些内容,那么您的唯一 id 生成代码可能应该只在非 AJAX 请求上运行,否则将现有 id 单独保留在会话中。通常,AJAX 请求有一个
HTTP_X_Requested_With
标头来区分它们。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.