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 的資料作為分頁的起點 |