简体   繁体   English

ASP.NET MVC-摆脱控制器构造函数中的请求? 或如何选择未处理异常的视图

[英]ASP.NET MVC - Bailing out of a request in the Controller Constructor? OR how to pick a view on unhandled exception

I have a scenario where in my controller's constructor, I have some logic that will make this request, die for sure. 我有一个场景,在我的控制器的构造函数中,我有一些逻辑可以使这个请求确定下来。

Is it possible to somehow invoke an action from the constructor itself, and then stop the Action that was originally going to take place? 是否有可能以某种方式从构造函数本身调用动作,然后停止原本会发生的动作?

Ideally, all I want to do is, call a View from my constructor if a condition is not met, or optionally call another Action which will call a specific view for me. 理想情况下,我要做的就是在不满足条件的情况下从构造函数中调用View,或者选择调用另一个Action为我调用特定的View。

The idea is, to not write the error checking code in each Action, since all the actions have one basic element to check. 这样做的想法是,不要在每个操作中编写错误检查代码,因为所有操作都需要检查一个基本元素。

Perhaps a custom IControllerFactory which handles specific exceptions thrown from the constructor? 也许是一个自定义IControllerFactory ,它处理构造函数抛出的特定异常?

See Custom Controller Factory in ASP.NET . 请参阅ASP.NET中的“自定义控制器工厂”

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

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