Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有 ACustomAuthorizeAttribute并且我Exception在HandleUnauthorizedRequest. 有什么方法可以捕获该异常并格式化消息并将其显示给用户。如果不是,那么应该如何实施这样的事情。
CustomAuthorizeAttribute
Exception
HandleUnauthorizedRequest
你想在全球范围内这样做吗?还是基于每个控制器?
如果是全局的,那么您可以创建自己的 HandleErrorAttribute 来捕获任何未处理的异常。然后,您可以测试异常是否为 HandleUnauthorizedRequest。
如果您想在每个控制器的基础上执行此操作,那么您可以覆盖 OnException 并在那里处理它。