ExtApiHelper API Reference
ExtApiHelper 是一個擴充自 .NET Web API 中 Controller 的類別,它提供了幾個常用的函式簡化你的程式碼
Struct
無,繼承自 ApiController 屬於 abstract 類型
這個擴充類別會內建一個 HttpResponseMessage 物件,有關的對應方法將作用在這個物件上
Property
無
Method
| 名稱 | 類型 | 描述 | 
|---|---|---|
| setHeader(string key, string value) | void | 根據指定的 key與value添加 Header | 
| addCookie(string key, string value, DateTime expired) | CookieHeaderValue | 根據指定的 key,value與expired(到期時間),添加 Cookie | 
| getHeader(string key) | string | 從 Request 中的 Header 回傳對應 key的數值,若無法取得則回傳null | 
| getIP() | string | 取得 Request 的來源 IP,無法取得時則回傳 null | 
| getUserAgent() | string | 取得 Request 的 User Agent,無法取得時回傳空字串 | 
| getMyHost() | string | 取得 API 目前的 Host,例如:https://api.com:123 | 
| getQuery(string key) | string | 從 Request 中的 QueryString 回傳對應 key的數值,若無法取得則回傳null | 
| getCookie(string key) | string | 從 Request 中的 Cookie 回傳對應 key的數值,若無法取得則回傳null | 
| HttpResponseMessage getResponse(object content, HttpStatusCode code) | HttpResponseMessage | 將 content轉換為 JSON 格式,並回傳HttpResponseMessage物件,code為 HTTP Status Code,預設為HttpStatusCode.OK | 
| getTextResponse(string content, HttpStatusCode code) | HttpResponseMessage | 將 content轉換為 Text 格式,並回傳HttpResponseMessage物件,code為 HTTP Status Code,預設為HttpStatusCode.OK | 
| getAttachmentResponse(string content, string filename, HttpStatusCode code) | HttpResponseMessage | 將 content轉換為檔案讓客戶端以下載方式接收,並回傳HttpResponseMessage物件,code為 HTTP Status Code,預設為HttpStatusCode.OK | 
| getStreamResponse(string file, string name, string type, string disposition) | HttpResponseMessage | 讀取 file檔案路徑的資料,讓客戶端以下載方式接收,並回傳HttpResponseMessage物件,name可以設定檔案名稱(預設 GUID 隨機給予),type為檔案的 MIME Type (預設application/octet-stream),disposition為下載模式(預設attachment) | 
| getStreamResponse(byte[] file, string name, string type, string disposition) | HttpResponseMessage | 將 file轉換為檔案讓客戶端以下載方式接收,並回傳HttpResponseMessage物件,name可以設定檔案名稱(預設 GUID 隨機給予),type為檔案的 MIME Type (預設application/octet-stream),disposition為下載模式(預設attachment) | 
| getRedirectResponse(string url, int second, string wording) | HttpResponseMessage | 輸出重新導向的 HTML 內容, url為新的導向目標,second為延遲幾秒 (預設1秒),wording為跳轉畫面上顯示的文字 (預設為跳轉中,請稍候...) | 
| uploadFile() | List<ModelFile> | 將 Request 中夾帶的檔案儲存至 Storage設定的目錄下,並回傳這些檔案的基本資料,name為新的檔案名稱,des為原始檔案名稱,size為檔案大小(byte),type為檔案的 MIME Type | 
| getFormBody<T> | T | 將 Request 中 Body 的 Form 資料綁定到指定的 Model (T) | 
| getJsonBody<T> | T | 將 Request 中 Raw Data 的 JSON 資料綁定到指定的 Model (T) | 
| addPaging(ref string sql, string orderby) | void | 將 sql添加傳統分頁語法,可在 Query String 中帶page頁數(預設1) 與limit筆數(預設50) 來定義取得的資料,orderby為欄位與排序方式,例如column desc | 
| addIdentityPaging(ref string sql, string orderby, string idcolumn, string nextId) | void | 將 sql添加以 ID 分頁的語法,可在 Query String 中帶limit筆數(預設50) 來定義取得的資料,orderby為欄位與排序方式(預設since desc),idcolumn為 ID 識別子的欄位名稱,nextId為基準 ID, 會以符合該 ID 的資料作為分頁的起點 |