Google Code Prettify

[WebAPI]REST Web Service 控制器的參考範例

這個範例是用 Visual Studio 2017 生成的, 放在這備忘, 也順便提供大家參考使用。


EmployeesController.cs
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;

using WebApplication2.Models;

namespace WebApplication2.Controllers {
    public class EmployeesController : ApiController
    {
        private MvcTestDbEntities db = new MvcTestDbEntities();

        // GET: api/Employees
        /// <summary>
        /// 取回所有 Employee 的資料明細
        /// </summary>
        /// <returns>IQueryable<employee></employee></returns>
        public IQueryable<employee> GetEmployee()
        {
            return db.Employee;
        }

        // GET: api/Employees/5
        /// <summary>
        /// 取回特定 id 的 Employee 資料
        /// </summary>
        /// <param name="id" />Employee ID
        /// <returns>typeof(Employee)</returns>
        [ResponseType(typeof(Employee))]
        public async Task<ihttpactionresult> GetEmployee(string id)
        {
            Employee employee = await db.Employee.FindAsync(id);
            if (employee == null)
            {
                return NotFound();
            }

            return Ok(employee);
        }

        // PUT: api/Employees/5
        /// <summary>
        /// 變更 Employee 資料
        /// </summary>
        /// <param name="id" />Employee ID
        /// <param name="employee" />Employee
        /// <returns>typeof(void)</returns>
        [ResponseType(typeof(void))]
        public async Task<ihttpactionresult> PutEmployee(string id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != employee.emp_no)
            {
                return BadRequest();
            }

            db.Entry(employee).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Employees
        /// <summary>
        /// 新增 Employee 資料
        /// </summary>
        /// <param name="employee" />Employee
        /// <returns>typeof(Employee)</returns>
        [ResponseType(typeof(Employee))]
        public async Task<ihttpactionresult> PostEmployee(Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Employee.Add(employee);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (EmployeeExists(employee.emp_no))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = employee.emp_no }, employee);
        }

        // DELETE: api/Employees/5
        /// <summary>
        /// 刪除 Employee 資料
        /// </summary>
        /// <param name="id" />Employee ID
        /// <returns>typeof(Employee)</returns>
        [ResponseType(typeof(Employee))]
        public async Task<ihttpactionresult> DeleteEmployee(string id)
        {
            Employee employee = await db.Employee.FindAsync(id);
            if (employee == null)
            {
                return NotFound();
            }

            db.Employee.Remove(employee);
            await db.SaveChangesAsync();

            return Ok(employee);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool EmployeeExists(string id)
        {
            return db.Employee.Count(e => e.emp_no == id) > 0;
        }
    }
}