使用ASP.NET?Web?API構(gòu)建Restful?API
一、前言
RESTful API 是基于HTTP協(xié)議產(chǎn)生的一種相對簡單的API設(shè)計方案;
RESTful 的核心是 everything is a “resource”,所有的HTTP action,都應(yīng)該是相應(yīng)resource上可以被操作和處理的,而API 就是對資源的管理操作,而這個具體操作是由 HTTP action 指定的。
使用HTTP的GET、POST、DELETE、PUT來表示對于資源的增刪改查。
- GET:讀?。≧ead)
- POST:新建(Create)
- PUT:更新(Update)
- DELETE:刪除(Delete)
學(xué)習(xí)資料準(zhǔn)備:
(1)用于測試的實(shí)體類:
public class Student
{
public string StuNo { get; set; } //學(xué)號
public string StuName { get; set; } //姓名
public string StuSex { get; set; } //性別
}(2)模擬的測試數(shù)據(jù)(編寫在webapi類的構(gòu)造函數(shù)中):
public static List<Student> StuList { get; set; }
public ServerController()
{
StuList = new List<Student>();
StuList.Add(new Student { StuNo = "001", StuName = "孫悟空", StuSex = "男" });
StuList.Add(new Student { StuNo = "002", StuName = "豬八戒", StuSex = "男" });
StuList.Add(new Student { StuNo = "003", StuName = "白骨精", StuSex = "女" });
}二、獲取所有數(shù)據(jù)
API接口:
[HttpGet]
public List<Student> GetList([FromBody]dynamic obj)
{
return StuList;
}三、獲取詳情
API接口:
[HttpGet]
public Student GetDetail(string id)
{
Student stu = StuList.SingleOrDefault(p => p.StuNo.Equals(id));
return stu;
}四、新增數(shù)據(jù)
API接口:
[HttpPost]
public IHttpActionResult Add([FromBody] Student stu)
{
StuList.Add(stu);
return Json(new { msg = "添加成功!",data=StuList });
}五、修改數(shù)據(jù)
API接口:
[HttpPut]
public IHttpActionResult Update(string id, [FromBody] Student stu)
{
Student model = StuList.SingleOrDefault(p => p.StuNo.Equals(id));
if (model == null)
return Json(new { msg = "修改的數(shù)據(jù)不存在!" });
model.StuNo = stu.StuNo;
model.StuName = stu.StuName;
model.StuSex = stu.StuSex;
return Json(new { msg = "修改成功!", data = model });
}六、刪除數(shù)據(jù)
API接口:
[HttpDelete]
public IHttpActionResult Delete(string id)
{
Student model = StuList.SingleOrDefault(p => p.StuNo.Equals(id));
if (model == null)
return Json(new { msg = "刪除的數(shù)據(jù)不存在!" });
StuList.Remove(model);
return Json(new { msg = "刪除成功!", data = StuList });
}七、特性路由
(1) 基本路由映射
API接口:
[HttpGet]
[Route("api/Route/Basic")]
public IHttpActionResult GetBasic()
{
return Json(new { msg = "測試基本的特性路由!" });
}接口訪問地址:
http://localhost:60650/api/Route/Basic
(2) 路由映射參數(shù)
API接口:
[HttpGet]
[Route("api/Route/Param/{id}/{name}")]
public IHttpActionResult Get2(string id,string name)
{
return Json(new { msg = "測試路由映射參數(shù)!",data = new { id= id,name=name } });
}接口訪問地址:
http://localhost:60650/api/Route/Param/001/孫悟空
(3) 多重特性路由
API接口:
[HttpGet]
[Route("api/Route/Multiple1/{id}/{name}")]
[Route("api/Route/Multiple2/{id}/{name}")]
public IHttpActionResult Get3(string id,string name)
{
return Json(new { msg = "測試多重特性路由!",data = new { id= id,name=name } });
}接口訪問地址:
http://localhost:60650/api/Route/Multiple1/001/孫悟空
或
http://localhost:60650/api/Route/Multiple2/001/孫悟空
(4) 缺省參數(shù)路由
API接口:
[HttpGet]
[Route("api/Route/Default/{id}/{name?}")]
public IHttpActionResult Get4(string id, string name="孫悟空")
{
return Json(new { msg = "測試缺省參數(shù)路由!", data = new { id = id, name = name } });
}或
[HttpGet]
[Route("api/Route/Default/{id}/{name=孫悟空}")]
public IHttpActionResult Get4(string id, string name)
{
return Json(new { msg = "測試缺省參數(shù)路由!", data = new { id = id, name = name } });
}接口訪問地址:
http://localhost:60650/api/Route/Default/001
(5) 參數(shù)約束路由
ASP.NET Web API內(nèi)置約束有下面這些:
{x:alpha} 約束大小寫英文字母
{x:bool}
{x:datetime}
{x:decimal}
{x:double}
{x:float}
{x:guid}
{x:int}
{x:length(6)}
{x:length(1,20)} 約束長度范圍
{x:long}
{x:maxlength(10)}
{x:min(10)}
{x:range(10,50)} //約束值的范圍
{x:regex(正則表達(dá)式)}可以設(shè)置多個約束:
[Route("api/Route/orders/{id:int:min(1)}")]API接口:
[HttpGet]
[Route("api/Route/Check/{id:range(1,100)}/{name}")]
public IHttpActionResult Get5(int id, string name)
{
return Json(new { msg = "測試參數(shù)約束路由!", data = new { id = id, name = name } });
}接口訪問地址:(以下地址可以正常訪問接口)
http://localhost:60650/api/Route/Check/11/孫悟空
接口訪問地址:(以下地址無法訪問接口)
http://localhost:60650/api/Route/Check/101/孫悟空
(6) 通配符(*)路由變量
API接口:
[HttpGet]
[Route("api/Route/Date/{id:range(1,100)}/{name}/{*birthday}")]
public IHttpActionResult Get6(int id, string name,DateTime birthday)
{
return Json(new { msg = "*使用!", data = new { id = id, name = name,birthday = birthday.ToString("yyyy年MM月dd日") } });
}接口訪問地址:
http://localhost:60650/api/Route/Date/1/jack/1988-8-8
接口訪問地址:(此地址如果在路由中沒有*則無法訪問,因為程序會認(rèn)為“1988/8/8”是三個參數(shù),而不是一個參數(shù))
http://localhost:60650/api/Route/Date/1/jack/1988/8/8
(7) 路由映射復(fù)雜參數(shù)
給實(shí)體類添加特性使其支持綁定:
[ModelBinder]
public class Student
{
public string StuNo { get; set; } //學(xué)號
public string StuName { get; set; } //姓名
public string StuSex { get; set; } //性別
}方案一API接口:
[HttpGet]
[Route("api/Route/Class/{stu.StuNo}/{stu.StuName}/{stu.StuSex}")]
public IHttpActionResult Get7(Student stu)
{
return Json(new { msg = "路由映射復(fù)雜參數(shù)!", data = stu });
}方案一接口訪問地址:
http://localhost:60650/api/Route/Class/001/jack/man
方案二API接口:
[HttpGet]
[Route("api/Route/Class")]
public IHttpActionResult Get7(Student stu)
{
return Json(new { msg = "路由映射復(fù)雜參數(shù)!", data = stu });
}方案二接口訪問地址:
http://localhost:60650/api/Route/Class?stu.StuNo=001&stu.StuName=jack&stu.StuSex=man
(8) 路由前綴
在控制器類上定義路由前綴:
[RoutePrefix("api/Prefix")]
public class PrefixController : ApiController
{
}路由前綴API接口:
[Route("Basic")]
public IHttpActionResult Get1()
{
return Json(new { msg = "測試路由前綴!" });
}接口訪問地址:
http://localhost:60650/api/Prefix/Basic
取消路由前綴API接口:
[Route("~/api/Cancel/Basic")] //通過~/取消路由前綴
public IHttpActionResult Get2()
{
return Json(new { msg = "取消路由前綴!" });
}接口訪問地址:
http://localhost:60650/api/Cancel/Basic
到此這篇關(guān)于使用ASP.NET Web API構(gòu)建Restful API的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
.net core 基于Hangfire+Mysql持久化實(shí)現(xiàn)定時任務(wù)配置方法
這篇文章主要介紹了.net core 基于Hangfire+Mysql持久化實(shí)現(xiàn)定時任務(wù)配置方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
ASP.NET中CKEditor與CKFinder的配置使用
這篇文章主要介紹了ASP.NET中CKEditor與CKFinder的配置使用的相關(guān)資料,需要的朋友可以參考下2015-06-06
理解ASP.NET Core 依賴注入(Dependency Injection)
把有依賴關(guān)系的類放到容器中,解析出這些類的實(shí)例,就是依賴注入。目的是實(shí)現(xiàn)類的解耦。本文主要介紹了ASP.NET Core 依賴注入(Dependency Injection),需要了解具體內(nèi)容的可以仔細(xì)閱讀這篇文章,希望對你有所幫助2021-09-09
.Net Core2.1 WebAPI新增Swagger插件詳解
這篇文章主要給大家介紹了關(guān)于.Net Core2.1 WebAPI新增Swagger插件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
ASP.NET網(wǎng)站偽靜態(tài)下使用中文URL的方法
中文URL是在URL中直接使用漢字,它的好處是可以使用鏈接地址看起來非常直觀易懂,偽靜態(tài)的規(guī)則,是在web.config文件中定義的2014-08-08

