跳到主要內容

.NET WebAPI 2 Custom Response Message

Imports System.Net
Imports System.Web.Http
Imports System.Net.Http
Imports Newtonsoft.Json
Imports System.Web.Http.Filters


''' <summary>
''' 登入驗證錯誤自訂訊息
''' </summary>
''' <remarks> </remarks>
Public Class TestAuthorizeAttribute
    Inherits AuthorizeAttribute

    Protected Overrides Sub HandleUnauthorizedRequest(actionContext As Controllers.HttpActionContext)
        MyBase.HandleUnauthorizedRequest(actionContext)

        actionContext.Response.Content = New StringContent(JsonConvert.SerializeObject(New Dictionary(Of String, String) From {{"result", "-1"}}, Formatting.Indented))

        Select Case actionContext.Response.StatusCode
            Case 401
                Dim dicError As Dictionary(Of String, String) =
                    New Dictionary(Of String, String) From {
                        {"result", "-1"},
                        {"msg", "Login First"}
                    }
                actionContext.Response.Content = New StringContent(JsonConvert.SerializeObject(dicError, Formatting.Indented))
            Case Else
                actionContext.Response.Content = New StringContent(JsonConvert.SerializeObject(New Dictionary(Of String, String) From {{"result", "-1"}}, Formatting.Indented))
        End Select
    End Sub

End Class

''' <summary>
''' 例外錯誤自訂訊息
''' </summary>
''' <remarks></remarks>
Public Class TestExceptionFilterAttribute
    Inherits ExceptionFilterAttribute

    Public Overrides Sub OnException(actionExecutedContext As HttpActionExecutedContext)
        MyBase.OnException(actionExecutedContext)
        Dim resp As New HttpResponseMessage
        resp.Content = New StringContent(JsonConvert.SerializeObject(New Dictionary(Of String, String) From {{"result", "-1"}, {"msg", "Custom Exception"}}, Formatting.Indented))
        actionExecutedContext.Response = resp
    End Sub

End Class

''' <summary>
''' 參數格式錯誤自訂訊息
''' </summary>
''' <remarks></remarks>
Public Class TestActionFilterAttribute
    Inherits ActionFilterAttribute

    Public Overrides Sub OnActionExecuting(actionContext As Controllers.HttpActionContext)
        MyBase.OnActionExecuting(actionContext)
        If Not actionContext.ModelState.IsValid Then
            Dim resp As New HttpResponseMessage
            resp.Content = New StringContent(JsonConvert.SerializeObject(New Dictionary(Of String, String) From {{"result", "-1"}, {"msg", "Format Error"}}, Formatting.Indented))
            actionContext.Response = resp
        End If
    End Sub
End Class


Public Class ValuesController
    Inherits ApiController

    ' GET api/values
    
    
    Public Function GetValues() As IEnumerable(Of String)
        Return New String() {"value1", "value2"}
    End Function

    ' GET api/values/5
    
    Public Function GetValue(ByVal id As Integer) As String
        Return "value"
    End Function

    ' POST api/values
    Public Sub PostValue( ByVal value As String)

    End Sub

    ' PUT api/values/5
    Public Sub PutValue(ByVal id As Integer,  ByVal value As String)

    End Sub

    ' DELETE api/values/5
    Public Sub DeleteValue(ByVal id As Integer)

    End Sub
End Class

實做登入機制
http://demo.tc/Post/534

留言

這個網誌中的熱門文章

刪除Office 2016金鑰的Command

前往安裝目錄 cd C:\Program Files (x86)\Microsoft Office\Office16 查詢已安裝金鑰 (會顯示金鑰末5碼) cscript OSPP.VBS /dstatus 刪除已安裝金鑰 ( XXXXX 為末5碼) cscript ospp.vbs /unpkey: XXXXX 重新啟動Office任一產品 or  安裝金鑰 cscript ospp.vbs /inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX 搞定 為什麼要特地記錄2016的刪除方法呢? 因為在  程式和功能  裡, 變更 無法更改金鑰

變更 Visual Studio 連接 TFS 密碼

開啟 認證管理員 選擇 Windows 認證 編輯並變更密碼 完成~
上回兄弟三人一起吃飯小聚 已經想不起來是什麼時候了 乘著老哥生日為由 三人一邊閒聊從通化街出發 帶著一張烏拉拉欣給我的免費套餐兌換卷 到仁愛圓環旁的晃士家豬排用餐 看到菜單先點了我最愛的玉子燒 哥與弟人先後點了二個豬排套餐 我的則是兌換的漢堡排套餐 開胃小菜的毛豆和冬瓜都不錯吃 算是與其它中價位豬排店比較為出色的部分 玉子燒是帶點鹹的味道 和杏子豬排的甜相較我偏好杏子豬排 (玉子燒就是要甜甜的~~) 至於主餐的部分就… 讓我感覺到杏子豬排跟勝博殿貴一點無所謂 XD 連照片都懶得放了 湯也可以加算是不錯的地方 喝起來還算順口 套餐附有飲料、甜點 我點的甜點是紅豆湯 大顆的口感不錯 也不會太甜 算是很有誠意的甜點 至於餐後飲料是青草茶 就…是青草茶 以玉子燒代蛋糕 結束了這次的慶生聚餐 然後… 消費滿700又拿到一張套餐兌換卷