跳到主要內容

.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

留言

這個網誌中的熱門文章

同事的 ISABellE 囍餅開箱

今天一早到公司就被已知的紅色bomb給炸了 粉紅色的提帶、粉紅色的盒身配上紫色的盒蓋 再綁上粉紅色的緞帶 給人一股少女懷春的夢幻感 打開盒蓋後發現 第一層是滿滿的餅乾 接著… 第二層也是滿滿的餅乾 (長得和第一層差不多,索性不拍了) 挑了巧克力餅乾、大巧克力餅乾、 巧克力球…等幾個種類來吃 不算太甜 嗯…是個內外兼具的好吃禮盒~

2011-01-20 Tokyo Travels First Day - 1 松山 vs 羽田

前言: 雖然不是第一次到東京 (這在我的另外幾個記述當中 大誤 XD) 由於本次的東京出遊規劃上有點倉促 直至出發前一天仍在搞定行程細目、下載Google Maps的快取圖資 深怕去了迷路或途中發生什麼狀況 果真驗證一句話:「活得愈老、膽子愈小。」 確認完畢躺在床上還是久久無法入眠 (其實是太興奮了) 這次前往東京比較特別的地方就是從松山機場出發 之前只記得和Byte在那邊拍過戲 若干年後再次踏進松山機場 發現它的確是變漂亮了 但是格局還是不若國際機場大器 由於這次行程買的是長榮的機加酒 一到長榮的櫃檯就有小姐為我們服務 拿過我們的護照就往一台配色很像ibon的機器掃一下 劃好位後我們很鎮定地接過護照說聲謝謝就去寄行李了 後來還是跑去偷偷照了那台很厲害的機器 (內心的OS:耶~一下子就劃好位了咧~科技真是日新月異) 第一次從羽田機場入關,有二個小感想: 一是不曉得安檢人員比較針對拿紅色護照的人還是剛好抽檢 排我們前面的仁兄居然遭到衣褲鞋、全身加行李翻查的高格規待遇 (心裡頓時萬分慶幸 I am from Taiwan) 二是羽田機場雖然沒有成田那麼大 不過正持續擴建中 飛機降落時可以看到填海造出來的飛機跑道 整個機場亮麗如新 跟我們重新整頓飛國際的松山機場相比 簡直是○○比×× (請自行填入) 再此由不得感佩 "呷胖尼子" 的做事風格 比較可惜的是行程排得太趕 沒多駐足羽田機場 (甚至連照片都沒拍 XD) 就急著出關找LAWSON跟7-11