如何拦截 MVC 应用程序中的所有控制器调用?
是否有一种快速方法可以拦截 MVC-3 中的所有控制器调用?
出于记录和测试的目的,我想构建一个工具,可以拦截所有控制器调用,并记录调用哪个控制器、使用哪个消息、在什么时间调用。
Is there a quick method for intercepting all controller calls in MVC-3?
For logging and testing purposes, I'd like to build a tool that can intercept all controller calls, and log which controller was called, with which message, at what time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不记得我从哪里得到这个,但我不久前在寻找类似的东西,发现一篇文章或某个地方包含这个日志过滤器:
要使用它,只需将其添加到 global.asax 中的全局过滤器中:
我现在去看看是否能找到来源。
编辑:找到了。这是来自这个问题。
I can't remember where I got this from, but I was looking around for something similar a while back and found an article or something somewhere that contained this logging filter:
To use it, just add it to the global filters in global.asax:
I'll have a look now to see if I can find the source.
Edit: Found it. It was from this question.
根据站点已有的大小,您可以在框架的
Controller
类和主控制器之间的层次结构中创建一个类。然后
你的控制器的其余部分可以继承这个,例如
Depending on how big the site is already, you could create a class in the hierarchy between the framework's
Controller
class and your main controllers.Something like
Then the rest of your controllers can inherit from this, e.g.
您也可以使用自己的控制器工厂并注册它:
来自:(网上有很多示例 - 在您想要的位置插入日志记录)
改编自: http://www.keyvan.ms/custom-controller-factory-in-asp-net-mvc
不要忘记在 global.asax.cs 中注册它
You can use your own controller factory and register it as well:
From: (many example on the net - insert logging where you want)
adapted from: http://www.keyvan.ms/custom-controller-factory-in-asp-net-mvc
dont forget to register it in global.asax.cs
有一个由 Phil Haack 开发的路由调试器
ASP.Net路由调试器
there is a routing debugger developed by Phil Haack
ASP.Net Routing Debugger