标签:
既然是 Web API,那么就会有不同的请求。 Http 的基本请求方法如下:
GET 请求获取Request-URI所标识的资源
POST 在Request-URI所标识的资源后附加新的数据
PUT 请求服务器存储一个资源,并用Request-URI作为其标识
DELETE 请求服务器删除Request-URI所标识的资源
以上4种方法,基本对应查询、新增、更新、删除 4种操作。
查询:
1)无参数:
public IEnumerable<Models.Employee> GetAllEmployees() { var list = new List<Models.Employee>(); using (var context = new NORTHWINDEntities()) { list = AutoMapper.Mapper.Map<System.Data.Entity.DbSet<DataAccess.Employee>, List<Models.Employee>>(context.Employees); } return list.ToArray(); }
2)有参数:
public IEnumerable<Models.Employee> GetById(int id) { var list = new List<Models.Employee>(); using (var context = new NORTHWINDEntities()) { var queryList = context.Employees.Where(q => q.EmployeeID == id).ToList(); list = AutoMapper.Mapper.Map<List<DataAccess.Employee>, List<Models.Employee>>(queryList); } return list.ToArray(); }
新增:
需要注意
1. [FromBody] 会告诉Web API 去Request 的Body 里取参数值,并且该属性在一个方法里只能出现一次
2. Post 如果提交的字段有多个值时,接收参数需要是一个Class
public Models.Employee Insert([FromBody]Models.Employee employee) { Models.Employee newEmployee = null; DataAccess.Employee dtoEmployee = AutoMapper.Mapper.Map<Models.Employee, DataAccess.Employee>(employee); using (var context = new NORTHWINDEntities()) { var newdto = context.Employees.Add(dtoEmployee); int id = context.SaveChanges(); newEmployee = AutoMapper.Mapper.Map<DataAccess.Employee, Models.Employee>(newdto); } return newEmployee; }
更新:
public Models.Employee Put([FromBody]Models.Employee employee) { Models.Employee updateEmployee = null; using (var context = new NORTHWINDEntities()) { var dtoEmployee = context.Employees.FirstOrDefault(q => q.EmployeeID == employee.EmployeeID); dtoEmployee.FirstName = employee.FirstName; dtoEmployee.LastName = employee.LastName; dtoEmployee.Title = employee.Title; context.SaveChanges(); updateEmployee = AutoMapper.Mapper.Map<DataAccess.Employee, Models.Employee>(dtoEmployee); } return updateEmployee; }
删除:
public void Delete(int id) { using (var context = new NORTHWINDEntities()) { var emp = context.Employees.FirstOrDefault(q => q.EmployeeID == id); context.Employees.Remove(emp); context.SaveChanges(); } }
前台页面调用代码:
<div id="body"> <h3>Get All</h3> <input type="button" id="getAll" value="GetAll" /> <div id="employeeAll"> </div> <h3>Get by Id</h3> <input type="text" id="employeeId" /> <input type="button" id="getById" value="GetById" /> <div id="employeeById"> </div> <h3>Add New Employee</h3> LastName: <input type="text" id="lastName" /> FirstName: <input type="text" id="firstName" /> Title: <input type="text" id="title" /> <input type="button" id="insertEmployee" value="Insert" /> <div id="newEmployee"> </div> <h3>Update Employee</h3> Id: <input type="text" id="uId" /> LastName: <input type="text" id="ulastName" /> FirstName: <input type="text" id="ufirstName" /> Title: <input type="text" id="utitle" /> <input type="button" id="updateEmployee" value="Update" /> <div id="updatedEmployee"> </div> <h3>Delete by Id</h3> <input type="text" id="delId" /> <input type="button" id="delById" value="Delete" /> <div id="afterDel"> </div> <input type="button" id="clearAll" value="Clear All" /> <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script type="text/ecmascript"> $().ready(function () { //查询所有记录 $("#getAll").click(function () { $.getJSON("/api/v1/Employees") .done(function (data) { var html = "<ul>"; $(data).each(function (i, item) { html += "<li>" + item.EmployeeID + " | " + item.LastName + " " + item.FirstName + " | " + item.Title + "</li>"; }); html += "</ul>"; $("#employeeAll").html(html); }); }); //带参数查询 $("#getById").click(function () { var id = $("#employeeId").val(); $.getJSON("/api/v1/Employees/" + id) .done(function (data) { var html = "<ul>"; $(data).each(function (i, item) { html += "<li>" + item.EmployeeID + " | " + item.LastName + " " + item.FirstName + " | " + item.Title + "</li>"; }); html += "</ul>"; $("#employeeById").html(html); }); }); //新增 $("#insertEmployee").click(function () { var employee = { lastname: $("#lastName").val(), firstname: $("#firstName").val(), title: $("#title").val() }; $.ajax({ type: "POST", url: "/api/v1/Employees", contentType: "application/json", data: JSON.stringify(employee), success: function (data) { var html = "<ul>"; $(data).each(function (i, item) { html += "<li>" + item.EmployeeID + " | " + item.LastName + " " + item.FirstName + " | " + item.Title + "</li>"; }); html += "</ul>"; $("#newEmployee").html(html); }, complete: function (XMLHttpRequest, textStatus) { //HideLoading(); }, error: function () { //请求出错处理 } }); }); //更新 $("#updateEmployee").click(function () { var employee = { employeeid: $("#uId").val(), lastname: $("#ulastName").val(), firstname: $("#ufirstName").val(), title: $("#utitle").val() }; $.ajax({ type: "PUT", url: "/api/v1/Employees", contentType: "application/json", data: JSON.stringify(employee), success: function (data) { var html = "<ul>"; $(data).each(function (i, item) { html += "<li>" + item.EmployeeID + " | " + item.LastName + " " + item.FirstName + " | " + item.Title + "</li>"; }); html += "</ul>"; $("#updatedEmployee").html(html); }, complete: function (XMLHttpRequest, textStatus) { //HideLoading(); }, error: function () { //请求出错处理 } }); }); //删除 $("#delById").click(function () { var id = $("#delId").val(); $.ajax({ type: "DELETE", url: "/api/v1/Employees/" + id, success: function (data) { var html = "<ul>"; $(data).each(function (i, item) { html += "<li>" + item.EmployeeID + " | " + item.LastName + " " + item.FirstName + " | " + item.Title + "</li>"; }); html += "</ul>"; $("#newEmployee").html(html); }, complete: function (XMLHttpRequest, textStatus) { //HideLoading(); }, error: function () { //请求出错处理 } }); }); }); </script> </div>
终于完成了第二篇,以上代码完成了最常见的CRUD 操作。
作为一名苦逼的程序猿,谨以此记录成长路上的点点滴滴。
标签:
原文地址:http://www.cnblogs.com/daxiong105/p/4649034.html