1:簡單介紹什么是Web api
REST屬于一種設(shè)計風(fēng)格瞎抛,REST 中的 POST(新增數(shù)據(jù))钓株,GET(取得數(shù)據(jù))您单,PUT(更新數(shù)據(jù))斋荞,DELETE(刪除數(shù)據(jù))來進(jìn)行數(shù)據(jù)庫的增刪改查,而如果開發(fā)人員的應(yīng)用程式符合REST原則虐秦,則它的服務(wù)為“REST風(fēng)格Web服務(wù)“也稱的RESRful Web API”平酿。
微軟的web api是在vs2012上的mvc4項(xiàng)目綁定發(fā)行的,它提出的web api是完全基于RESTful標(biāo)準(zhǔn)的悦陋,完全不同于之前的(同是SOAP協(xié)議的)wcf和webService蜈彼,它是簡單,代碼可讀性強(qiáng)的俺驶,上手快的幸逆,如果要拿它和web服務(wù)相比,我會說暮现,它的接口更標(biāo)準(zhǔn)还绘,更清晰,沒有混亂的方法名稱栖袋,有的只有幾種標(biāo)準(zhǔn)的請求拍顷,如get,post,put,delete等,它們分別對應(yīng)的幾個操作塘幅,下面講一下:
GET:生到數(shù)據(jù)列表(默認(rèn))菇怀,或者得到一條實(shí)體數(shù)據(jù)
POST:添加服務(wù)端添加一條記錄,記錄實(shí)體為Form對象
PUT:添加或修改服務(wù)端的一條記錄晌块,記錄實(shí)體的Form對象爱沟,記錄主鍵以GET方式進(jìn)行傳輸
DELETE:刪除 服務(wù)端的一條記錄
2、WebApi特點(diǎn)
1)類必須繼承ApiController
2)返回類型不再是ActionResult
3)默認(rèn)是請求WebApi控制器中的和HttpMethod同名的方法
設(shè)置當(dāng)前WebApi的默認(rèn)返回格式為json--移除xml格式
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
3匆背、步入正題
我們在正常使用的WebApi呼伸,都是可以一鍵生成“增(POST)--刪(DELETE)--改(PUT)--查(GET)”對應(yīng)的方法,但是都是僅限于單表操作钝尸,在項(xiàng)目需求下是遠(yuǎn)遠(yuǎn)不夠的括享,有時候需要對多張表進(jìn)行操作,本文僅作兩張表進(jìn)行講解珍促,多表操作其實(shí)也是一個道理铃辖,照葫蘆畫瓢,你不會猪叙?別逗~~
環(huán)境:
數(shù)據(jù)庫:SQL2012
IDE:VS2012
數(shù)據(jù)庫怎么連以及怎么生成模型自行百度娇斩,不會也可以私聊我仁卷,在這就不廢話了,生成模型后(模型至少要有兩張表喲~)
如圖:
然后新建一個模型類犬第,把你需要拿到的字段屬性寫到模型類里锦积,如圖:
TestModel.cs如下代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ng2GetDataTest.Models.Model
{
public class TestModel
{
public int ID { get; set; }
public string ProcessID { get; set; }
public Nullable<System.DateTime> aCreateTime { get; set; }
public Nullable<System.DateTime> aModifyTime { get; set; }
public string Applicant { get; set; }
public string Department { get; set; }
public double InvoiceValue { get; set; }
public double PayMoney { get; set; }
public string PayModel { get; set; }
public string PayWho { get; set; }
public string BankAccount { get; set; }
public string ApplyStatus { get; set; }
public string PayType { get; set; }
public string DomainAccount { get; set; }
public string UserTitleImg { get; set; }
public string UserName { get; set; }
public string UserPassword { get; set; }
public Nullable<System.DateTime> bCreateTime { get; set; }
public Nullable<System.DateTime> bModifyTime { get; set; }
}
}
說白了也就是自動生成的模型里,set歉嗓、get的代碼
建立好后,新建一個空白控制器丰介,繼承與系統(tǒng)默認(rèn)的ApiController
private DbTestEntities2 db = new DbTestEntities2();
// GET api/GetDataTest
public IEnumerable<Models.Model.TestModel> GetDataTest()
{
List<Models.Model.TestModel> rec = (from a in db.UserInfo
join b in db.ApplyInfo on a.ProcessID equals b.ProcessID
select new Models.Model.TestModel
{
ID = a.ID,
ProcessID = a.ProcessID,
DomainAccount = a.DomainAccount,
UserTitleImg = a.UserTitleImg,
UserName = a.UserName,
UserPassword = a.UserPassword,
aCreateTime = a.CreateTime,
aModifyTime = a.ModifyTime,
bCreateTime = b.CreateTime,
bModifyTime = b.ModifyTime,
Applicant = b.Applicant,
Department = b.Department,
InvoiceValue = b.InvoiceValue,
PayMoney = b.PayMoney,
PayModel = b.PayModel,
PayWho = b.PayWho,
BankAccount = b.BankAccount,
ApplyStatus = b.ApplyStatus,
PayType = b.PayType
}).ToList();
return rec;
}
}
}
注意:List<Models.Model.TestModel> rec = (from a in db.UserInfo
join b in db.ApplyInfo on a.ProcessID equals b.ProcessID //這塊是多表查詢語句,對應(yīng)剛才新建模型里的字段查詢
數(shù)據(jù)庫數(shù)據(jù)如下:
第一張表
第二張表
對應(yīng)查詢出來的數(shù)據(jù):
[
{
"ID": 1,
"ProcessID": "121vg5fg4-fsdf-fsdfk",
"aCreateTime": "2017-02-17T00:00:00",
"aModifyTime": null,
"Applicant": "裴大敏",
"Department": "IT",
"InvoiceValue": 100,
"PayMoney": 200,
"PayModel": "支付寶轉(zhuǎn)賬 ",
"PayWho": "上海豐誠物業(yè)管理有限公司",
"BankAccount": "604545646",
"ApplyStatus": "Applying ",
"PayType": "付款申請 ",
"DomainAccount": "489347378@qq.com",
"UserTitleImg": null,
"UserName": "裴大敏",
"UserPassword": "peidamin",
"bCreateTime": "2017-02-20T00:00:00",
"bModifyTime": "2017-02-21T00:00:00"
},
{
"ID": 1,
"ProcessID": "121vg5fg4-fsdf-fsdfk",
"aCreateTime": "2017-02-17T00:00:00",
"aModifyTime": null,
"Applicant": "裴中敏",
"Department": "IT",
"InvoiceValue": 100,
"PayMoney": 300,
"PayModel": "銀聯(lián)轉(zhuǎn)賬 ",
"PayWho": "上海豐誠物業(yè)管理有限公司",
"BankAccount": "604545646",
"ApplyStatus": "Applyed ",
"PayType": "付款申請 ",
"DomainAccount": "489347378@qq.com",
"UserTitleImg": null,
"UserName": "裴大敏",
"UserPassword": "peidamin",
"bCreateTime": "2017-02-15T00:00:00",
"bModifyTime": null
},
{
"ID": 1,
"ProcessID": "121vg5fg4-fsdf-fsdfk",
"aCreateTime": "2017-02-17T00:00:00",
"aModifyTime": null,
"Applicant": "裴小敏",
"Department": "IT",
"InvoiceValue": 100,
"PayMoney": 500,
"PayModel": "支付寶轉(zhuǎn)賬 ",
"PayWho": "上海豐誠物業(yè)管理有限公司",
"BankAccount": "604545646",
"ApplyStatus": "Refused ",
"PayType": "付款申請 ",
"DomainAccount": "489347378@qq.com",
"UserTitleImg": null,
"UserName": "裴大敏",
"UserPassword": "peidamin",
"bCreateTime": "2017-02-25T00:00:00",
"bModifyTime": null
}
]
好了鉴分,這次的教程希望能幫助到需要的朋友哮幢,可能會有點(diǎn)簡單,但我想還是會有人需要的志珍,在學(xué)習(xí)ASP.NET過程中橙垢,記錄一下,免得日后忘了碴裙,有需要源碼可以到這里來拿:https://github.com/AganYa/GetModelDataDemo