web api中post引數時只能乙個

2022-02-28 11:37:29 字數 1015 閱讀 5404

在webapi中,請求主體(httpcontent)

只能被讀取一次,不被快取,只能向前讀取的流。

舉例子說明:

/?id=123&name=bob 

void action(int id, string name) // 所有引數都是簡單型別,因而都將來自url

/?id=123&name=bob 

void action([fromuri] int id, [fromuri] string name) // 同上

void action([frombody] string name); //[formbody]特性顯示標明讀取整個body為乙個字串作為引數

public class customer  

public int age  

}/?id=123 

void action(int id, customer c) // 引數id從query string中讀取,引數c是乙個複雜customer物件類戲,通過formatter從body中讀取

void action(customer c1, customer c2) // 出錯!多個引數都是複雜型別,都試圖從body中讀取,而body只能被讀取一次

void action([fromuri] customer c1, customer c2) // 可以!不同於上面的action,複雜型別c1將從url中讀取,c2將從body中讀取

void action([modelbinder(mycustombinder)] sometype c) // 標示使用特定的model binder來解析引數

[modelbinder(mycustombinder)] public class sometype // 通過給特定型別sometype宣告標註[modelbidner(mycustombinder)]特性使得所有sometype型別引數應用此規則 

void action(sometype c) // 由於c的型別為sometype,因而應用sometype上的特性決定其採用model binding

webapi 自宿主 post 多個引數

這種方式支援post 一次傳遞多個引數,如果用jsonstringfy 只能傳遞乙個資料,這樣的弊端是,引數不明確。而且為了後台轉換方便,你必須單獨new乙個物件,這樣無疑既麻煩 又不明確。這種方式後台需要方法接收引數型別是 jobject型別。然後需要先序列化,再反序列化。注意 如果上面的data...

WebApi認證刷選器獲取Post引數

使用webapi再所難免會遇到認證需求,畢竟沒有認證的服務安全隱患太大。我們在webapi中進行認證基本上都是新增乙個刷選器,繼承authorizeattribute。繼承後需要重寫handleunauthorizedrequest方法實現攔截上下文,攔截後可對本次請求進行驗證。如果你的webapi...

WebApi中的引數傳遞

webapi引數傳遞總結 在webapi中,請求主體 httpcontent 只能被讀取一次,不被快取,只能向前讀取的流。舉例子說明 服務端方法 void action int id,string name 所有引數都是簡單型別,因而都將來自url 服務端方法 void action fromuri...