Table of Contents

Class ExtApiHelper

Namespace
ZapLib
Assembly
ZapLib.dll

Web API 控制存取輔助工具

public class ExtApiHelper
Inheritance
ExtApiHelper
Inherited Members

Constructors

ExtApiHelper(ApiController)

建構子,請傳入目前的 ApiController (this) 實體

public ExtApiHelper(ApiController api = null)

Parameters

api ApiController

目前這個 Controller 物件

Properties

cookies

設定在 response 的 cookie, 只允許在內部 AddCookie 進行設定

public List<CookieHeaderValue> cookies { get; }

Property Value

List<CookieHeaderValue>

encoding

Response 的 Encoding,預設 Encoding.UTF8

public Encoding encoding { get; set; }

Property Value

Encoding

request

初始請求物件

public HttpRequestMessage request { get; }

Property Value

HttpRequestMessage

resp

最終回應物件

public HttpResponseMessage resp { get; }

Property Value

HttpResponseMessage

Methods

AddCookie(string, string, DateTime)

設定回應內容的 Cookie 數值

public CookieHeaderValue AddCookie(string key, string value, DateTime expired)

Parameters

key string

名稱

value string

數值

expired DateTime

過期時間

Returns

CookieHeaderValue

設定好的 Cookie 物件

AddIdentityPaging(ref string, string, string, string)

增加 SQL 以某個具備識別的欄位數值為基準進行翻頁的語法

public void AddIdentityPaging(ref string sql, string orderby = "since desc", string idcolumn = null, string nextId = null)

Parameters

sql string

SQL 語法

orderby string

欄位名稱

idcolumn string

具備識別的欄位名稱

nextId string

翻頁 ID

AddPaging(ref string, string)

增加 SQL 翻頁語法

[Obsolete("這個方法可能在下個版本中棄用")]
public void AddPaging(ref string sql, string orderby = "asc")

Parameters

sql string

SQL 語法

orderby string

欄位名稱

GetAttachmentResponse(string, string, HttpStatusCode)

取得 API 回應物件,並將內容字串以串流方式回應 (客戶將以下載檔案方式取得文字)

public HttpResponseMessage GetAttachmentResponse(string content, string filename = null, HttpStatusCode code = HttpStatusCode.OK)

Parameters

content string

內容字串

filename string

檔案名稱

code HttpStatusCode

回應的 HTTP 狀態碼,預設 200

Returns

HttpResponseMessage

Web API 回應物件

GetCookie(string)

取得客戶請求中特定名稱的 Cookie 數值

public string GetCookie(string key)

Parameters

key string

特定名稱

Returns

string

Cookie 數值,取不到時為 NULL

GetFormBody<T>()

取得客戶請求中的 multi-part form 資料部分,並反序列化綁定到指定的資料模型中

public T GetFormBody<T>()

Returns

T

綁定好資料的資料模型

Type Parameters

T

指定的資料模型型態 T

GetHeader(string, string)

從客戶的請求中取出指定名稱的 Header 數值,如果取不到則回傳預設值

public string GetHeader(string key, string def_val = null)

Parameters

key string

指定名稱

def_val string

預設數值

Returns

string

Header 數值,取不到時回傳 NULL

GetHeader<T>(string, T)

從客戶的請求中取出指定名稱的 Header 數值,並嘗試轉換成指定型態 T,如果取不到或轉換不過則回傳預設值

public T GetHeader<T>(string key, T def_val = default)

Parameters

key string

指定名稱

def_val T

預設數值

Returns

T

Type Parameters

T

指定轉換的型態

GetHeaders()

從客戶的請求中取出所有 Headers

public Dictionary<string, IEnumerable<string>> GetHeaders()

Returns

Dictionary<string, IEnumerable<string>>

所有 Header 的字典集合

GetIP()

取得客戶請求的來源 IP

public string GetIP()

Returns

string

IP 字串

GetJsonBody<T>()

取得客戶請求中的 JSON 資料部分,並反序列化綁定到指定的資料模型中

public T GetJsonBody<T>()

Returns

T

綁定好資料的資料模型

Type Parameters

T

指定的資料模型型態

GetMyHost()

取得客戶請求的 URL 字串

public string GetMyHost()

Returns

string

URL 字串

GetQuery(string)

取得客戶請求查詢字串中指定的名稱數值

public string GetQuery(string key)

Parameters

key string

指定名稱

Returns

string

指定名稱的數值

GetRedirectResponse(string, int, string)

取得 API 回應物件,並將使用者導向到新的 URL 位置

public HttpResponseMessage GetRedirectResponse(string url, int second = 1, string wording = "跳轉中,請稍候...")

Parameters

url string

導向到新的 URL

second int

延遲秒數,預設 1 秒

wording string

顯示文字,預設為 "跳轉中,請稍候..."

Returns

HttpResponseMessage

Web API 回應物件

GetResponse(object, HttpStatusCode)

取得 API 回應物件,並將內容物件邊碼成 JSON 格式

public HttpResponseMessage GetResponse(object content = null, HttpStatusCode code = HttpStatusCode.OK)

Parameters

content object

內容物件

code HttpStatusCode

回應的 HTTP 狀態碼,預設 200

Returns

HttpResponseMessage

Web API 回應物件

GetStreamResponse(byte[], string, string, string)

取得 API 回應物件,並將指定byte[] 二進位資料以串流方式回應 (客戶將以下載檔案方式取得資料)

public HttpResponseMessage GetStreamResponse(byte[] file, string name = null, string type = "application/octet-stream", string disposition = "attachment")

Parameters

file byte[]

二進位資料

name string

檔案名稱,預設為隨機產生亂碼

type string

回應的內容格式,預設為 application/octet-stream

disposition string

下載模式,預設為 attachment ,強迫以下載方式取得檔案

Returns

HttpResponseMessage

Web API 回應物件

GetStreamResponse(Stream, string, string, string)

取得 API 回應物件,並將指定路徑的檔案以串流方式回應 (客戶將以下載檔案方式取得檔案)

public HttpResponseMessage GetStreamResponse(Stream stream, string name = null, string type = "application/octet-stream", string disposition = "attachment")

Parameters

stream Stream

檔案串流,當串流無法讀取時 404 Not Found 的 Status Code

name string

檔案名稱,預設為隨機產生亂碼

type string

回應的內容格式,預設為 application/octet-stream

disposition string

下載模式,預設為 attachment ,強迫以下載方式取得檔案

Returns

HttpResponseMessage

Web API 回應物件

GetStreamResponse(string, string, string, string)

取得 API 回應物件,並將指定路徑的檔案以串流方式回應 (客戶將以下載檔案方式取得檔案)

public HttpResponseMessage GetStreamResponse(string file, string name = null, string type = "application/octet-stream", string disposition = "attachment")

Parameters

file string

檔案路徑,當檔案不存在時將回傳 404 Not Found 的 Status Code

name string

檔案名稱,預設為隨機產生亂碼

type string

回應的內容格式,預設為 application/octet-stream

disposition string

下載模式,預設為 attachment ,強迫以下載方式取得檔案

Returns

HttpResponseMessage

Web API 回應物件

GetTextResponse(string, HttpStatusCode)

取得 API 回應物件,並將內容保持純文字格式

public HttpResponseMessage GetTextResponse(string content = null, HttpStatusCode code = HttpStatusCode.OK)

Parameters

content string

內容物件

code HttpStatusCode

回應的 HTTP 狀態碼,預設 200

Returns

HttpResponseMessage

Web API 回應物件

GetUserAgent()

取得客戶請求的瀏覽器版本

public string GetUserAgent()

Returns

string

瀏覽器版本字串

SetHeader(string, string)

設定回應內容的 Header 數值

public void SetHeader(string key, string value)

Parameters

key string

名稱

value string

數值

UploadFile()

將客戶請求中的 multi-part form 檔案部分儲存到 .config 中設定的 Storage 路徑下,並回傳該檔案的基本資訊模型物件

public List<ModelFile> UploadFile()

Returns

List<ModelFile>

檔案基本資訊資料模型