Introduction

This document is for ASP.NET MVC and Web API. If you're interested in ASP.NET Core, see ASP.NET Core documentation.

In a web application, exceptions are usually handled in MVC Controller actions and Web API Controller actions. When an exception occurs, application user somehow informed about the error and optionally reason of the error.

在Web应用程序中,异常通常是在MVC控制器操作和Web API控制器操作中处理的。当异常发生时,应用程序用户被告知错误和错误的可选原因。

If an error occured in a regular HTTP request, an error page is shown. If an error occured in an AJAX request, server sends error information to the client and then client handles and shows the error to the user.

如果在常规HTTP请求中出现错误,则显示错误页面。如果Ajax请求中出现错误,服务器向客户端发送错误信息,然后客户端处理并向用户显示错误。

Handling exceptions in all web request is a tedious and repeating work. ASP.NET Boilerplate automates this. You almost never need to explicitly handle any exception. ASP.NET Boilerplate handles all exceptions, logs them and returns appropriate and formatted response to the client. Also, handles these response in the client and show error messages to the user.

处理所有Web请求中的异常是一项单调乏味的重复工作。ASP.NET样板自动化这个。您几乎从不需要显式地处理任何异常。ASP.NET样板处理所有的异常,日志,返回适当格式的响应到客户端。此外,在客户机中处理这些响应并向用户显示错误消息。

Enabling Error Handling

To enable error handling for ASP.NET MVC Controllers, customErrors mode must be enabled for ASP.NET MVC applications.

<customErrors mode="On" />

It also can be 'RemoteOnly' if you do not want to handler errors in local computer. Note that this is only required for ASP.NET MVC Controllers, not needed for Web API Controllers.

If you have already handling exceptions in a global filter then it may hide exceptions and ABP's exception handling may not work as you expected. So, if you do that, do it carefully.

它也可以“remoteonly”如果你不想在本地计算机处理程序错误。请注意,这仅仅是ASP.NET所需的MVC控制器,Web API控制器不需要。

如果您已经在全局筛选器中处理异常,则它可能隐藏异常,而ABP的异常处理可能不象您预期的那样工作。所以,如果你这样做,要小心。

Non-Ajax Requests

If request is not AJAX, an error page is shown.

Showing Exceptions

Here, there is an MVC controller action which throws an arbitrary exception:

这里有一个MVC控制器动作,它抛出一个任意异常:

public ActionResult Index()
{
throw new Exception("A sample exception message...");
}

Surely, this exception could be thrown by another method that is called from this action. ASP.NET Boilerplate handles this exception, logs it and shows 'Error.cshtml' view. You can customize this view to show the error. An example Error view (default Error view in ASP.NET Boilerplate templates):

ASP.NET Boilerplate hides details of the exception from users and shows a standard (and localizable) error message, unless you explicitly throw a UserFriendlyException.

UserFriendlyException

UserFriendlyException is a special type of exception that is directly shown to the user. See the sample below:

public ActionResult Index()
{
throw new UserFriendlyException("Ooppps! There is a problem!", "You are trying to see a product that is deleted...");
}

ASP.NET Boilerplate logs it but does not hide exception in this time:

So, if you want to show a special error message to users, just throw a UserFriendlyException (or an exception derived from it).

Error Model

ASP.NET Boilerplate passes an ErrorViewModel object as model to the Error view:

public class ErrorViewModel
{
public AbpErrorInfo ErrorInfo { get; set; } public Exception Exception { get; set; }
}

ErrorInfo contains detailed informations about the error that can be shown to the user. Exception object is the thrown exception. You can check it and show additional informations if you want. For example, we can show validation errors if it's an AbpValidationException:

AJAX Requests

If return type of MVC action is JsonResult (or Task<JsonResult for async actions), ASP.NET Boilerplate returns a JSON object to the client on exceptions. Sample return object for an error:

{
"targetUrl": null,
"result": null,
"success": false,
"error": {
"message": "An internal error occured during your request!",
"details": "..."
},
"unAuthorizedRequest": false
}

success: false indicates that there is an error. error object provides error message and details.

When you use ASP.NET Boilerplate's infrastructure to make AJAX request in client side, it automatically handles this JSON object and shows error message to the user using message API. See AJAX API for more information.

Exception Event

When ASP.NET Boilerplare handles any exception, it triggers AbpHandledExceptionData event that can be registered to be informed (See eventbus documentation for more information about Event Bus). Example:

public class MyExceptionHandler : IEventHandler<AbpHandledExceptionData>, ITransientDependency
{
public void HandleEvent(AbpHandledExceptionData eventData)
{
//TODO: Check eventData.Exception!
}
}

If you put this example class into your application (generally into your Web project), HandleEvent method will be called for all exceptions handled by ASP.NET Boilerplate. So, you can investigate the Exception object in detail.

ABP框架系列之二十八:(Handling-Exceptions-异常处理)的更多相关文章

  1. ABP框架系列之二十:(Dependency-Injection-依赖注入)

    What is Dependency Injection If you already know Dependency Injection concept, Constructor and Prope ...

  2. ABP框架系列之二十六:(EventBus-Domain-Events-领域事件)

    In C#, a class can define own events and other classes can register it to be notified when something ...

  3. ABP框架系列之二十二:(Dynamic-Web-API-动态WebApi)

    Building Dynamic Web API Controllers This document is for ASP.NET Web API. If you're interested in A ...

  4. ABP框架系列之二十四:(Email-Sending-EF-电子邮件发送)

    Introduction Email sending is a pretty common task for almost every application. ASP.NET Boilerplate ...

  5. ABP框架系列之二十五:(Embedded-Resource-Files-嵌入式资源文件)

    Introduction ASP.NET Boilerplate provides an easy way of using embedded Razor views (.cshtml files) ...

  6. ABP框架系列之二十九:(Hangfire-Integration-延迟集成)

    Introduction Hangfire is a compherensive background job manager. You can integrate ASP.NET Boilerpla ...

  7. ABP源码分析二十八:ABP.MemoryDB

    这个模块简单,且无实际作用.一般实际项目中都有用数据库做持久化,用了数据库就无法用这个MemoryDB 模块了.原因在于ABP限制了UnitOfWork的类型只能有一个(前文以作介绍),一般用了数据库 ...

  8. ABP框架系列之二十七:(Feature-Management-特征管理)

    Introduction Most SaaS (multi-tenant) applications have editions (packages) those have different fea ...

  9. ABP框架系列之二:(Entity Framework Core-实体核心框架)

    Introduction(介绍) Abp.EntityFrameworkCore nuget package is used to integrate to Entity Framework (EF) ...

随机推荐

  1. C语言 练习题

    subString #include <iostream> int subString(char* sSeek, char* sKey) { char* p = sSeek; while( ...

  2. 使用fdisk进行分区

    fdisk进行分区 1.首先使用fdisk -l 发现待分区磁盘/dev/vdb  大小为1TB 2.fdisk /dev/vdb 对该磁盘进行分区,输入m并回车 3.输入n并回车,n是“new”新建 ...

  3. dubbo实现原理介绍

      一.什么是dubbo Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,     ...

  4. 在线安装TIDB集群

     在线安装TiDB集群 服务器准备 说明:TiDB8需要能够连接外网,以便下载各类安装包 TiDB4非必须,但最好是有一台,因为后续测试Mysql数据同步或者进行性能比较时,都要用到 TiKV最好是采 ...

  5. CMake,win10,64位,简单配置测试

    https://cmake.org/download/ 下载完成后,解压即可. 创建文件夹,文件路径自己选择: 这里,就近选择在桌面--创建HelloWorld档,在该文档下,分别创建CMakeLis ...

  6. uva-10602-贪心

    题意:有个编辑器,支持三种操作,摁下一个键盘上的字符,重复最后一个单词,删除最后一个字符.给N个字符串,必须先在编辑器内输入第一个字符, 问,输入完所有字符串最少需要摁下多少次键盘. 最多100个字符 ...

  7. 27.Socket,TCP,UDP,HTTP基本通信原理

    Socket,TCP,UDP,HTTP基本通信原理(摘自百度): TCP.UDP,HTTP 底层通信都是通过 socket 套接字实现 网络上不同的计算机,也可以通信,那么就得使用网络套接字(sock ...

  8. 【学习】通用函数:快速的元素级数组函数【Numpy】

    通用函数(即ufunc)是一种对ndarray中的数据执行元素级运算的函数.可以将其看做简单函数(接受一个或多个标量值,并产生一个或多个标量值)的矢量化包装器. sqrt 和 exp为一元(unary ...

  9. shell:实现linux服务器资源监控并发送告警邮件

    1.安装方式 wget http://10.8.225.126/wsmonitor/install.sh;sh install.sh test@test.com 2.install.sh #!/bin ...

  10. ARCore中Pose类变换点的算法实现

    ARCore中Pose类变换点的算法实现,主要分为两步,分别是平移和旋转. 1. 旋转向量:通过四元数计算旋转后的向量 参数列表:q表示四元数, v是长度为4的float数组,表示待旋转的向量,   ...