【C#】MVC WEB API 語法架構
ASP.NET WEB API架構是以.NET Framework基礎,可用來建置RESTful【註1】應用程式,可建置HTTP服務並提供用戶端瀏覽器和行動裝置應用程式使用。
WEB API相較於Web Services和WCF來說,Web Services和WCF的資料內容不易辨識且傳送資料過於肥大,而WEB API提供XML和JSON的傳輸格式,同時可透過HTTP操作方法GET、POST、PUT、DELETE來操作服務,並提供輕量化與跨平台的特性。
WEB API相較於Web Services和WCF來說,Web Services和WCF的資料內容不易辨識且傳送資料過於肥大,而WEB API提供XML和JSON的傳輸格式,同時可透過HTTP操作方法GET、POST、PUT、DELETE來操作服務,並提供輕量化與跨平台的特性。
本篇透過範例方式建立支援CRUD操作WEB API,在Controllers加入控制器"具有讀取/寫入 Web Api 2"之WEB API控制器。
部分WEB API控制器之程式碼(CustomerController.cs僅供參考)︰
using System.Linq; //引用命名空間-LINQ
using prjView.Models; //引用命名空間-專案名稱.Models
public class CustomerController : ApiController //WEB API必須繼承自ApiController類別。
{
dbCustomerEntities db=new dbCustomerEntities(); //建立存取資料庫類別物件db
//當瀏覽器輸入「http://localhost/api/Customer」,會對此WEB API發出Get請求,Action方法回傳查詢結果
// GET: api/Customer
public List<tCustomer> Get()
{
var customers = db.tCustomer; //EX: tCustomer資料表
return customers.ToList();
}
public List<tCustomer> Get()
{
var customers = db.tCustomer; //EX: tCustomer資料表
return customers.ToList();
}
//當瀏覽器輸入「http://localhost/api/Customer?fid=2」,會對此WEB API發出Get請求並傳入fid參數值為2,Action方法回傳查詢結果
// GET: api/Customer/5
public tCustomer Get(int fid)
{
var customer = db.tCustomer.Where(m=>m.fId == fid).FirstOrDefault();
return customer;
}
// GET: api/Customer/5
public tCustomer Get(int fid)
{
var customer = db.tCustomer.Where(m=>m.fId == fid).FirstOrDefault();
return customer;
}
//當瀏覽器輸入「http://localhost/api/Customer」,送出Post請求後,WEB API取得用戶端表單(Form)欄位資料,新增一筆紀錄
// POST: api/Customer
public int Post(string fname, string fphone, string femail, string faddress)
{
int num = 0;
try
{
tCustomer customer = new tCustomer();
customer.fName = fname;
customer.fPhone = fphone;
customer.fEmail = femail;
customer.fAddress = faddress;
db.tCustomer.Add(customer);
num = db.SaveChanges();
}
catch (Exception ex)
{
num = 0;
}
return num;
}
// POST: api/Customer
public int Post(string fname, string fphone, string femail, string faddress)
{
int num = 0;
try
{
tCustomer customer = new tCustomer();
customer.fName = fname;
customer.fPhone = fphone;
customer.fEmail = femail;
customer.fAddress = faddress;
db.tCustomer.Add(customer);
num = db.SaveChanges();
}
catch (Exception ex)
{
num = 0;
}
return num;
}
//當瀏覽器輸入「http://localhost/api/Customer」,送出Put請求後,WEB API取得用戶端表單(Form)欄位資料,修改一筆紀錄
// PUT: api/Customer/5
public int Put(int fid, string fname, string fphone, string femail, string faddress)
{
int num = 0;
try
{
var customer = db.tCustomer.Where(m => m.fId == fid).FirstOrDefault();
customer.fName = fname;
customer.fPhone = fphone;
customer.fEmail = femail;
customer.fAddress = faddress;
num = db.SaveChanges();
}
catch (Exception ex)
{
num = 0;
}
return num;
}
// PUT: api/Customer/5
public int Put(int fid, string fname, string fphone, string femail, string faddress)
{
int num = 0;
try
{
var customer = db.tCustomer.Where(m => m.fId == fid).FirstOrDefault();
customer.fName = fname;
customer.fPhone = fphone;
customer.fEmail = femail;
customer.fAddress = faddress;
num = db.SaveChanges();
}
catch (Exception ex)
{
num = 0;
}
return num;
}
//當瀏覽器輸入「http://localhost/api/Customer?fid=2」,送出Delete請求後,WEB API取得fid參數值為2,刪除指定紀錄。
// DELETE: api/Customer/5
public int Delete(int fId)
{
int num = 0;
try
{
var customer = db.tCustomer.Where(m => m.fId == fId).FirstOrDefault();
db.tCustomer.Remove(customer);
num = db.SaveChanges();
}
catch (Exception ex)
{
num = 0;
}
return num;
}
// DELETE: api/Customer/5
public int Delete(int fId)
{
int num = 0;
try
{
var customer = db.tCustomer.Where(m => m.fId == fId).FirstOrDefault();
db.tCustomer.Remove(customer);
num = db.SaveChanges();
}
catch (Exception ex)
{
num = 0;
}
return num;
}
}
【註1】︰
REST(Representational State Transfer)表現層狀態轉換,是設計風格而不是標準,資源是由URI來指定,意旨於URL中所呈現的網址,對資源的操作包括取得、建立、修改和刪除,這些操作正好對應HTTP協定提供的GET、POST、PUT和DELETE方法。
留言
張貼留言