简体   繁体   English

捕获CustomAuthorizeAttribute ASP.NET MVC 4引发的异常

[英]catching exception thrown By CustomAuthorizeAttribute ASP.NET MVC 4

Suppose I have A CustomAuthorizeAttribute and I thew Exception in HandleUnauthorizedRequest . 假设我有一个CustomAuthorizeAttribute和我thew ExceptionHandleUnauthorizedRequest 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.

相关问题 ASP.NET MVC:控制器未捕获自定义Identity UserStore类中引发的异常 - ASP.NET MVC: Controller not catching exception thrown in custom Identity UserStore class 捕获ASP.NET WebService上WebMethod引发的自定义异常 - Catching a custom Exception thrown by a WebMethod on ASP.NET WebService 即使用户已登录,CustomAuthorizeAttribute也会重定向到LoginView(ASP.Net MVC 5) - CustomAuthorizeAttribute redirects to LoginView even when the user is logged in (ASP.Net MVC 5) 在ASP.NET Core MVC中捕获异常 - Catching exceptions in ASP.NET Core MVC 在ASP.NET MVC和C#中使用自定义异常类时未捕获异常 - Exception not catching when using Custom Exception classes in ASP.NET MVC and C# asp.net中的页面重定向引发的异常 - Exception thrown by page redirection in asp.net 创建控制器时“调用目标已抛出异常”(ASP.NET MVC 5 with MySql) - “Exception has been thrown by target of invocation” when creating controller (ASP.NET MVC 5 with MySql) 捕获asp.net mvc的所有响应。 有什么选择? - Catching asp.net mvc all the responses. What are the options? 在 ASP.NET MVC 中使用 SweetAlert 进行捕获错误 - Make a catching error using SweetAlert in ASP.NET MVC ASP.NET MVC捕捉和在视图中使用参数 - ASP.NET MVC Catching and Using Parameter In The View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM