ASP.NET Core AutoWrapper 自定義響應輸出

来源:https://www.cnblogs.com/yyfh/archive/2020/03/30/12602087.html
-Advertisement-
Play Games

前言 AutoWrapper是一個簡單可自定義全局異常處理程式和ASP.NET Core API響應的包裝。他使用ASP.NET Core middleware攔截傳入的HTTP請求,並將最後的結果使用統一的格式來自動包裝起來.目的主要是讓我們更多的關註業務特定的代碼要求,並讓包裝器自動處理HTTP ...


前言

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)\r\n   at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)\r\n …"
    }
}

如果想將某些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)\r\n   at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)\r\n …"
    }
}

請註意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響應模型定義為以下格式

public class MyCustomApiResponse  
{
    public int Code { get; set; }
    public string Message { get; set; }
    public object Payload { get; set; }
    public DateTime SentDate { get; set; }
    public Pagination Pagination { get; set; }

    public MyCustomApiResponse(DateTime sentDate, object payload = null, string message = "", int statusCode = 200, Pagination pagination = null)
    {
        this.Code = statusCode;
        this.Message = message == string.Empty ? "Success" : message;
        this.Payload = payload;
        this.SentDate = sentDate;
        this.Pagination = pagination;
    }

    public MyCustomApiResponse(DateTime sentDate, object payload = null, Pagination pagination = null)
    {
        this.Code = 200;
        this.Message = "Success";
        this.Payload = payload;
        this.SentDate = sentDate;
        this.Pagination = pagination;
    }

    public MyCustomApiResponse(object payload)
    {
        this.Code = 200;
        this.Payload = payload;
    }

}

public class Pagination  
{
    public int TotalItemsCount { get; set; }
    public int PageSize { get; set; }
    public int CurrentPage { get; set; }
    public int TotalPages { get; set; }
}

通過如下代碼片段進行測試結果

public async Task<MyCustomApiResponse> Get()  
{
    var data = await _personManager.GetAllAsync();

    return new MyCustomApiResponse(DateTime.UtcNow, data,
        new Pagination
        {
            CurrentPage = 1,
            PageSize = 10,
            TotalItemsCount = 200,
            TotalPages = 20
        });

}

運行後會得到如下影響格式


{
    "code": 200,
    "message": "Success",
    "payload": [
        {
            "id": 1,
            "firstName": "Vianne",
            "lastName": "Durano",
            "dateOfBirth": "2018-11-01T00:00:00"
        },
        {
            "id": 2,
            "firstName": "Vynn",
            "lastName": "Durano",
            "dateOfBirth": "2018-11-01T00:00:00"
        },
        {
            "id": 3,
            "firstName": "Mitch",
            "lastName": "Durano",
            "dateOfBirth": "2018-11-01T00:00:00"
        }
    ],
    "sentDate": "2019-10-17T02:26:32.5242353Z",
    "pagination": {
        "totalItemsCount": 200,
        "pageSize": 10,
        "currentPage": 1,
        "totalPages": 20
    }
}

但是從這裡要註意一旦我們對API響應進行自定義,那麼就代表我們完全控制了要格式化數據的方式,同時丟失了預設API響應的某些選項配置。但是我們仍然可以利用ApiException()方法引發用戶定義的錯誤消息
如下所示

[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


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • title: Java基礎語法(5) 特殊流程式控制制語句 blog: "CSDN" data: "Java學習路線及視頻" 1.嵌套迴圈結構 將一個迴圈放在另一個迴圈體內,就形成了嵌套迴圈。其中,for ,while ,do…while均可以作為外層迴圈或內層迴圈。 實質上,嵌套迴圈就是把內層迴圈當成 ...
  • title: Java基礎語法(4) 流程式控制制 blog: "CSDN" data: "Java學習路線及視頻" 1.程式流程式控制制 流程式控制制語句是用來控製程序中各語句執行順序的語句,可以把語句組合成能完成一定功能的小邏輯模塊。 三種基本流程結構 順序結構 程式從上到下逐行地執行,中間沒有任何判斷和跳 ...
  • 需求分析 在分享源碼之前,先將b2b2c系統中許可權模塊的需求整理、明確,方便源碼的理解。 業務需求 b2b2c電子商務系統中許可權主要有三個角色:買家、賣家、平臺管理員。 其中賣家角色中又有店員,可以設置店員管理不同的許可權(如商品和訂單的許可權分派給不同的店員),同理平臺管理員也需要進行上述精細許可權的管 ...
  • 一、無監督學習基礎知識 利用無標簽的數據學習數據的分佈或數據與數據之間的關係被稱作無監督學習 有監督學習和無監督學習的最大區別在於數據是否有標簽 無監督學習最常應用的場景是聚類(Clustering)和降維(Dimension Reduction) 二、聚類 聚類是根據數據的“相似性”將數據分為多類 ...
  • 有的人說 Python 入門容易,但是精通難的語言,這點我非常贊同。 Python 語言里有許多(而且是越來越多)的高級特性,是 Python 發燒友們非常喜歡的。在這些人的眼裡,能夠寫出那些一般開發者看不懂的高級特性,就是高手,就是大神。 但你要知道,在團隊合作里,炫技是大忌。 為什麼這麼說呢?我 ...
  • 目錄 在日常開發中,對數據進行序列化和反序列化,是常備的操作。而在Python標準庫中提供了json模塊對JSON數據的處理功能。 什麼是json? JSON(JavaScript Object Notation)是一種使用廣泛的輕量數據格式,相對於XML而言更簡單,也易於閱讀和編寫,機器也方便解析 ...
  • 前言 Python爬蟲要經歷爬蟲、爬蟲被限制、爬蟲反限制的過程。當然後續還要網頁爬蟲限制優化,爬蟲再反限制的一系列道高一尺魔高一丈的過程。爬蟲的初級階段,添加headers和ip代理可以解決很多問題。 PS:如有需要Python學習資料的小伙伴可以加點擊下方鏈接自行獲取http://t.cn/A6Z ...
  • a>使用的軟體是vs2017和sqlServer2012,使用的ASP.NET控制項是sqlDataSource、dropDownlist和UpdatePanel. b>打開sqlServer,以window身份驗證登錄,建立省份表與市區表,如圖1.1和圖1.2 圖1.1 省份表 圖1.2 市區表 c ...
一周排行
    -Advertisement-
    Play Games
  • C#TMS系統代碼-基礎頁面BaseCity學習 本人純新手,剛進公司跟領導報道,我說我是java全棧,他問我會不會C#,我說大學學過,他說這個TMS系統就給你來管了。外包已經把代碼給我了,這幾天先把增刪改查的代碼背一下,說不定後面就要趕鴨子上架了 Service頁面 //using => impo ...
  • 委托與事件 委托 委托的定義 委托是C#中的一種類型,用於存儲對方法的引用。它允許將方法作為參數傳遞給其他方法,實現回調、事件處理和動態調用等功能。通俗來講,就是委托包含方法的記憶體地址,方法匹配與委托相同的簽名,因此通過使用正確的參數類型來調用方法。 委托的特性 引用方法:委托允許存儲對方法的引用, ...
  • 前言 這幾天閑來沒事看看ABP vNext的文檔和源碼,關於關於依賴註入(屬性註入)這塊兒產生了興趣。 我們都知道。Volo.ABP 依賴註入容器使用了第三方組件Autofac實現的。有三種註入方式,構造函數註入和方法註入和屬性註入。 ABP的屬性註入原則參考如下: 這時候我就開始疑惑了,因為我知道 ...
  • C#TMS系統代碼-業務頁面ShippingNotice學習 學一個業務頁面,ok,領導開完會就被裁掉了,很突然啊,他收拾東西的時候我還以為他要旅游提前請假了,還在尋思為什麼回家連自己買的幾箱飲料都要叫跑腿帶走,怕被偷嗎?還好我在他開會之前拿了兩瓶芬達 感覺感覺前面的BaseCity差不太多,這邊的 ...
  • 概述:在C#中,通過`Expression`類、`AndAlso`和`OrElse`方法可組合兩個`Expression<Func<T, bool>>`,實現多條件動態查詢。通過創建表達式樹,可輕鬆構建複雜的查詢條件。 在C#中,可以使用AndAlso和OrElse方法組合兩個Expression< ...
  • 閑來無聊在我的Biwen.QuickApi中實現一下極簡的事件匯流排,其實代碼還是蠻簡單的,對於初學者可能有些幫助 就貼出來,有什麼不足的地方也歡迎板磚交流~ 首先定義一個事件約定的空介面 public interface IEvent{} 然後定義事件訂閱者介面 public interface I ...
  • 1. 案例 成某三甲醫預約系統, 該項目在2024年初進行上線測試,在正常運行了兩天後,業務系統報錯:The connection pool has been exhausted, either raise MaxPoolSize (currently 800) or Timeout (curren ...
  • 背景 我們有些工具在 Web 版中已經有了很好的實踐,而在 WPF 中重新開發也是一種費時費力的操作,那麼直接集成則是最省事省力的方法了。 思路解釋 為什麼要使用 WPF?莫問為什麼,老 C# 開發的堅持,另外因為 Windows 上已經裝了 Webview2/edge 整體打包比 electron ...
  • EDP是一套集組織架構,許可權框架【功能許可權,操作許可權,數據訪問許可權,WebApi許可權】,自動化日誌,動態Interface,WebApi管理等基礎功能於一體的,基於.net的企業應用開發框架。通過友好的編碼方式實現數據行、列許可權的管控。 ...
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...