如何在 MVC 中挂钩调用控制器操作?
在我的 MVC3 应用程序中,我有很多控制器和操作。有时执行操作时会引发异常。我希望能够记录该异常并让 MVC 处理异常。
像这样的伪代码:
try {
MvcInvokeControllerAction( controller, action, params );
} catch( Exception e ) {
trace( "Error while invoking " + controller + "-" +
action + " with " + params + " details: " + getDetails( e ) );
throw;
}
我想要的是能够在 MVC 首次处理异常之前捕获异常,因为有时 MVC 启动会引发另一个异常,我在 Application_Error()
和原始代码中看到后者例外是很多。
我如何实现这样的挂钩?
In my MVC3 application I have lots of controllers and actions. Sometimes an exception is raised while executing an action. I want to be able to log that and let the exception be handled by MVC.
Something like this pseudocode:
try {
MvcInvokeControllerAction( controller, action, params );
} catch( Exception e ) {
trace( "Error while invoking " + controller + "-" +
action + " with " + params + " details: " + getDetails( e ) );
throw;
}
What I want is to be able to catch the exception before it is first processed by MVC because sometimes MVC kicking in raises another exception and I see the latter in Application_Error()
and the original exception is lots.
How do I achieve such hooking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以覆盖 OnException 方法 在您的Basecontrollor 上。
这将是控制器操作中所有异常的 Catch 块。 中 Exetnd 所有控制器即可
只需从 Basecontrollor示例
You can override OnException Method On your Basecontrollor.
This will be a Catch block for all exceptions in Controllor Actions. just Exetnd all your Controllors from Basecontrollor
Example