加入收藏 | 设为首页 | 会员中心 | 我要投稿 淮安站长网 (https://www.0517zz.cn/)- 运营、云管理、经验、智能边缘、云硬盘!
当前位置: 首页 > 建站 > 正文

ASP.NET Core AutoWrapper 自定义响应输出实现

发布时间:2020-08-21 13:11:19 所属栏目:建站 来源:网络整理
导读:副标题#e# AutoWrapper是一个简单可自定义全局异常处理程序和ASP.NET Core API响应的包装。他使用ASP.NET Core middleware拦截传入的HTTP请求,并将最后的结果使用统一的格式来自动包装起来.目的主要是让我们更多的关注业务特定的代码要求,并让包装器自动

[Route("{id:long}")] [HttpPut] public async Task<MyCustomApiResponse> Put(long id, [FromBody] PersonDTO dto) { if (ModelState.IsValid) { try { var person = _mapper.Map<Person>(dto); person.ID = id; if (await _personManager.UpdateAsync(person)) return new MyCustomApiResponse(DateTime.UtcNow, true, "Update successful."); else throw new ApiException($"Record with id: {id} does not exist.", 400); } catch (Exception ex) { _logger.Log(LogLevel.Error, ex, "Error when trying to update with ID:{@ID}", id); throw; } } else throw new ApiException(ModelState.AllErrors()); }

现在当进行模型验证时,可以获得默认响应格式

{ "isError": true, "responseException": { "exceptionMessage": "Request responded with validation error(s). Please correct the specified validation errors and try again.", "validationErrors": [ { "field": "FirstName", "message": "'First Name' must not be empty." } ] } }

Reference

https://github.com/proudmonkey/AutoWrapper

到此这篇关于ASP.NET Core AutoWrapper 自定义响应输出实现的文章就介绍到这了,更多相关ASP.NET Core AutoWrapper响应输出内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

(编辑:淮安站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读