加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

ASP.NET Core AutoWrapper 自界说相应输出实现

发布时间:2020-08-21 08:58:44 所属栏目:编程 来源:网络整理
导读:这篇文章首要先容了ASP.NET Core AutoWrapper 自界说相应输出实现,文中通过示例代码先容的很是具体,对各人的进修可能事变具有必然的参考进修代价,必要的伴侣
副问题[/!--empirenews.page--]

AutoWrapper是一个简朴可自界说全局非常处理赏罚措施和ASP.NET Core API相应的包装。他行使ASP.NET Core middleware拦截传入的HTTP哀求,并将最后的功效行使同一的名目来自动包装起来.目标首要是让我们更多的存眷营业特定的代码要求,并让包装器自动处理赏罚HTTP相应。这可以在构建API时加速开拓时刻,同时为HTTP相应试试我们同一的尺度。

安装

AutoWrapper.Core从NuGet或通过CLI下载并安装

PM> Install-Package AutoWrapper.Core

在Startup.cs Configure要领中注册以下内容,可是牢记要放在UseRouting前

app.UseApiResponseAndExceptionWrapper();

启动属性映射

默认环境下AutoWrapper将在乐成哀求乐成时输出以下名目:

{ "message": "Request successful.", "isError": false, "result": [ { "id": 7002, "firstName": "Vianne", "lastName": "Durano", "dateOfBirth": "2018-11-01T00:00:00" } ] }

假如说不喜好默认属性定名方法,那么我们可以通过AutoWrapperPropertyMap属性举办映射为我们必要指定的任何名称。譬喻我么可以将result属性的名称变动为data。如下所示

public class MapResponseObject { [AutoWrapperPropertyMap(Prop.Result)] public object Data { get; set; } }

然后将MapResponseObject类转达给AutpWrapper middleware

app.UseApiResponseAndExceptionWrapper<MapResponseObject>();

通过映射从头哀求后,此刻影响名目如下所示

{ "message": "Request successful.", "isError": false, "data": { "id": 7002, "firstName": "Vianne", "lastName": "Durano", "dateOfBirth": "2018-11-01T00:00:00" } }

可以从中看出result属性已经改换为data属性了

默认环境下AutoWrapper产生非常时将吐出以下相应名目

{ "isError": true, "responseException": { "exceptionMessage": "Unhandled Exception occurred. Unable to process the request." } }

并且假如在AutoWrapperOptions中配置了IsDebug,则将发生带有仓库跟踪信息的相同信息

{ "isError": true, "responseException": { "exceptionMessage": " Input string was not in a correct format.", "details": " at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)rn at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)rn …" } }

假如想将某些APIError属性名称变动为其他名称,只必要在以下代码中添加以下映射MapResponseObject

public class MapResponseObject { [AutoWrapperPropertyMap(Prop.ResponseException)] public object Error { get; set; } [AutoWrapperPropertyMap(Prop.ResponseException_ExceptionMessage)] public string Message { get; set; } [AutoWrapperPropertyMap(Prop.ResponseException_Details)] public string StackTrace { get; set; } }

通过如下代码来模仿错误

int num = Convert.ToInt32("10s");

此刻映射后的输出如下所示

{ "isError": true, "error": { "message": " Input string was not in a correct format.", "stackTrace": " at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)rn at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)rn …" } }

请留意APIError此刻按照MapResponseObject类中界说的属性变动了模子的默认属性。

我们可以自由的选择映射任何属性,下面是映射属性相对应的列表

[AutoWrapperPropertyMap(Prop.Version)] [AutoWrapperPropertyMap(Prop.StatusCode)] [AutoWrapperPropertyMap(Prop.Message)] [AutoWrapperPropertyMap(Prop.IsError)] [AutoWrapperPropertyMap(Prop.Result)] [AutoWrapperPropertyMap(Prop.ResponseException)] [AutoWrapperPropertyMap(Prop.ResponseException_ExceptionMessage)] [AutoWrapperPropertyMap(Prop.ResponseException_Details)] [AutoWrapperPropertyMap(Prop.ResponseException_ReferenceErrorCode)] [AutoWrapperPropertyMap(Prop.ResponseException_ReferenceDocumentLink)] [AutoWrapperPropertyMap(Prop.ResponseException_ValidationErrors)] [AutoWrapperPropertyMap(Prop.ResponseException_ValidationErrors_Field)] [AutoWrapperPropertyMap(Prop.ResponseException_ValidationErrors_Message)]

自界说错误架构

AutoWrapper还提供了一个APIException可用于界说本身的非常的工具,假如想抛出本身的非常动静,则可以简朴地执行以下操纵

throw new ApiException("Error blah", 400, "511", "http://blah.com/error/511");

默认输格外式如下所示

{ "isError": true, "responseException": { "exceptionMessage": "Error blah", "referenceErrorCode": "511", "referenceDocumentLink": "http://blah.com/error/511" } }

虽然我们可以自界说错误名目

public class MapResponseObject { [AutoWrapperPropertyMap(Prop.ResponseException)] public object Error { get; set; } } public class Error { public string Message { get; set; } public string Code { get; set; } public InnerError InnerError { get; set; } public Error(string message, string code, InnerError inner) { this.Message = message; this.Code = code; this.InnerError = inner; } } public class InnerError { public string RequestId { get; set; } public string Date { get; set; } public InnerError(string reqId, string reqDate) { this.RequestId = reqId; this.Date = reqDate; } }

然后我们可以通过如下代码举办激发我们错误

throw new ApiException( new Error("An error blah.", "InvalidRange", new InnerError("12345678", DateTime.Now.ToShortDateString()) ));

输格外式如下所示

{ "isError": true, "error": { "message": "An error blah.", "code": "InvalidRange", "innerError": { "requestId": "12345678", "date": "10/16/2019" } } }

行使自界说API相应名目

假如映射满意不了我们的需求。而且我们必要向API相应模子中添加其他属性,那么我们此刻可以自界说本身的名目类,通过配置UseCustomSchema为true来实现,代码如下所示

app.UseApiResponseAndExceptionWrapper(new AutoWrapperOptions { UseCustomSchema = true });

此刻假设我们想在主API中相应中包括一个属性SentDate和Pagination工具,我们也许但愿将API相应模子界说为以下名目

(编辑:湖南网)

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

热点阅读