[英]catching exception thrown By CustomAuthorizeAttribute ASP.NET MVC 4
Suppose I have A CustomAuthorizeAttribute
and I thew Exception
in HandleUnauthorizedRequest
. 假设我有一个
CustomAuthorizeAttribute
和我thew Exception
的HandleUnauthorizedRequest
。 Is there any way to catch that exception and format the message and display it to the user. 有什么方法可以捕获该异常并格式化消息并将其显示给用户。 If not than how should a thing like this be implemented.
如果不是这样的事情应该如何实施。
Do you want to do this globally? 您想在全球范围内这样做吗? Or on a per controller basis?
还是基于每个控制器?
If Globally, then you can create your own HandleErrorAttribute which will catch any unhandled exceptions. 如果是全局设置,则可以创建自己的HandleErrorAttribute,它将捕获任何未处理的异常。 You can then test if the exception is HandleUnauthorizedRequest or not.
然后,您可以测试该异常是否为HandleUnauthorizedRequest。
If you want to do this on a per controller basis, then you can override OnException and handle it there. 如果要基于每个控制器执行此操作,则可以重写OnException并在那里进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.