跳到主要內容

.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的刪除方法呢? 因為在  程式和功能  裡, 變更 無法更改金鑰

連假(≠廉價)的三峽

乘著清明連假回家掃墓前的空檔 和Ulaulaulala走訪了一趟陌生又帶那麼點熟悉的三峽 印象中最近一次到訪 是和Bytes為了迎新宿營場地的事到處奔走時路經 真正有走入老街則是小大一迎新的時候了 (好久遠) 初到三峽老街時看到滿是排隊人潮 不曉得是連假所致還是一般假期就是如此 只是覺得如此古意滿盈的街道塞滿人以後 不就是傳藝中心嗎 XD 頓時心中有點失落… 走到三月湧老街的入口 由於肚子很餓 隨便找了間警察局對面的滷味攤買了花干、豆干、三片海帶 結果付帳的時候居然聽說:「謝謝 80 塊」!!!!!!!! 是我吃米不知米價還是最近含碘食物漲價 差一點想打電話請消保官來關切 後來想想 …觀光景點嘛 也不是天天來或小姑娘 貴一點也…算了 遠遠看起來整條街道相當有整體感 但是仔細看還是有發現各家裝潢及門牌都自成一格 其實老街有很多有特色店但是裡這間讓我印象最為深刻 因為有二: 一是在我拍它門口擺飾的時候 我的相機因為記憶卡存取失敗以後掛點了 這之前拍的照片全部無法讀取 囧 二是裡頭的店員說道: 剛才消保官去(這間店)旁邊那家排隊排得很誇張的滷味 說他們賣的東西太貴了,要限期改善... 聽得我真是心有戚戚 (煙~) 除上述二點之外,這家店的Fu還是跟其他店家不太一樣啦 店員穿著比較日式可愛風...不是重點 其實是它很有自由之丘那邊店家的感覺...裡頭如果有加賣蛋糕就更像了 另一個讓很多人駐足的地方是一家賣金牛角的屋頂 請別問我哪一家 裡頭太多家了 頂上聚集了很多麻雀吱吱喳喳 底下圍繞著一堆蔡頭咔擦咔擦 好不熱鬧 最後離開前不免俗地跟古意小道來張一張 別問我為什麼沒有合照 因為在一次記憶卡錯誤的災難下消失了 (照片全部是倒序法拍攝而成...消失後一張張拍回來的) 總之三峽是個平日不錯遛躂的好去處 看是要坐捷運轉公車、公車轉公車、公車不轉公車都可以到 端看客倌出發前要哪一道囉~