1:简单介绍什么是Web api
REST属于一种设计风格,REST 中的 POST(新增数据),GET(取得数据),PUT(更新数据),DELETE(删除数据)来进行数据库的增删改查,而如果开发人员的应用程式符合REST原则,则它的服务为“REST风格Web服务“也称的RESRful Web API”。
微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码可读性强的,上手快的,如果要拿它和web服务相比,我会说,它的接口更标准,更清晰,没有混乱的方法名称,有的只有几种标准的请求,如get,post,put,delete等,它们分别对应的几个操作,下面讲一下:
GET:生到数据列表(默认),或者得到一条实体数据
POST:添加服务端添加一条记录,记录实体为Form对象
PUT:添加或修改服务端的一条记录,记录实体的Form对象,记录主键以GET方式进行传输
DELETE:删除 服务端的一条记录
2、WebApi特点
1)类必须继承ApiController
2)返回类型不再是ActionResult
3)默认是请求WebApi控制器中的和HttpMethod同名的方法
设置当前WebApi的默认返回格式为json--移除xml格式
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
3、步入正题
我们在正常使用的WebApi,都是可以一键生成“增(POST)--删(DELETE)--改(PUT)--查(GET)”对应的方法,但是都是仅限于单表操作,在项目需求下是远远不够的,有时候需要对多张表进行操作,本文仅作两张表进行讲解,多表操作其实也是一个道理,照葫芦画瓢,你不会?别逗~~
环境:
数据库:SQL2012
IDE:VS2012
数据库怎么连以及怎么生成模型自行百度,不会也可以私聊我,在这就不废话了,生成模型后(模型至少要有两张表哟~)
如图:
然后新建一个模型类,把你需要拿到的字段属性写到模型类里,如图:
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 aCreateTime { get; set; }
public Nullable 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 bCreateTime { get; set; }
public Nullable bModifyTime { get; set; }
}
}
说白了也就是自动生成的模型里,set、get的代码
建立好后,新建一个空白控制器,继承与系统默认的ApiController
private DbTestEntities2 db = new DbTestEntities2();
// GET api/GetDataTest
public IEnumerable GetDataTest()
{
List 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 rec = (from a in db.UserInfo
join b in db.ApplyInfo on a.ProcessID equals b.ProcessID //这块是多表查询语句,对应刚才新建模型里的字段查询
数据库数据如下:
第一张表
第二张表
对应查询出来的数据:
[
{
"ID": 1,
"ProcessID": "121vg5fg4-fsdf-fsdfk",
"aCreateTime": "2017-02-17T00:00:00",
"aModifyTime": null,
"Applicant": "裴大敏",
"Department": "IT",
"InvoiceValue": 100,
"PayMoney": 200,
"PayModel": "支付宝转账 ",
"PayWho": "上海丰诚物业管理有限公司",
"BankAccount": "604545646",
"ApplyStatus": "Applying ",
"PayType": "付款申请 ",
"DomainAccount": "[email protected]",
"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": "银联转账 ",
"PayWho": "上海丰诚物业管理有限公司",
"BankAccount": "604545646",
"ApplyStatus": "Applyed ",
"PayType": "付款申请 ",
"DomainAccount": "[email protected]",
"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": "支付宝转账 ",
"PayWho": "上海丰诚物业管理有限公司",
"BankAccount": "604545646",
"ApplyStatus": "Refused ",
"PayType": "付款申请 ",
"DomainAccount": "[email protected]",
"UserTitleImg": null,
"UserName": "裴大敏",
"UserPassword": "peidamin",
"bCreateTime": "2017-02-25T00:00:00",
"bModifyTime": null
}
]
好了,这次的教程希望能帮助到需要的朋友,可能会有点简单,但我想还是会有人需要的,在学习ASP.NET过程中,记录一下,免得日后忘了,有需要源码可以到这里来拿:https://github.com/AganYa/GetModelDataDemo