基于 RESTful 标准的 Web Api
来源:zhao.yj
微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码可读性强的,上手快的,如果要拿它和web服务相比,我会说,它的接口更标准,更清晰,没有混乱的方法名称,有的只有几种标准的请求,如get,post,put,delete等,它们分别对应的几个操作,下面讲一下:
GET:生到数据列表(默认),或者得到一条实体数据
POST:添加服务端添加一条记录,记录实体为Form对象
PUT:添加或修改服务端的一条记录,记录实体的Form对象,记录主键以GET方式进行传输
DELETE:删除 服务端的一条记录
注意上面公开的API接口都是在XMLHttpRequest情况下调用的,当然你可以使用jquery的ajax组件来完成这个请求调用,它的代码更加面向对象,下面举例说明一下
这是页面的HTML代码部分,实现最基本的CRUD操作
<fieldset>
<legend>测试Web Api
</legend>
<a href=“javascript:add()”>添加(post)</a>
<a href=“javascript:update(1)”>更新(put)</a>
<a href=“javascript:deletes(1)”>删除(delete)</a>
<a href=“/api/test”>列表(Get)</a>
<a href=“/api/test/1”>实体(Get)</a>
</fieldset>
<script>
function add() {
.</span><span style="color:teal;font-size:inherit;font-family:inherit;font-weight:inherit;">ajax</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">({</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">url</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> : </span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"/api/Test/"</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">,</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">type</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> : </span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"POST"</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">,</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">data</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> :</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">{</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"UserID"</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;">:</span><span style="color:#009999;font-size:inherit;font-family:inherit;font-weight:inherit;">4</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">,</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"UserName"</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;">:</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"test"</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">,</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"UserEmail"</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;">:</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"Parry@cnblogs.com"</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">},</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">success</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;">: </span><span style="color:#800080;font-size:inherit;font-family:inherit;font-weight:bold;">function</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">(</span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">data</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">)</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">{</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="font-size:inherit;font-family:inherit;font-weight:bold;">alert</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">(</span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">JSON</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">.</span><span style="color:teal;font-size:inherit;font-family:inherit;font-weight:inherit;">stringify</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">(</span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">data</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">));</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">}</span></span></p><p><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">});</span></span></p><p><span style="font-size:12px;"> </span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">}</span></span></p><p><span style="font-size:12px;"> </span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#999999;font-size:inherit;font-family:inherit;font-weight:inherit;font-style:italic;">//更新</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#800080;font-size:inherit;font-family:inherit;font-weight:bold;">function</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:teal;font-size:inherit;font-family:inherit;font-weight:inherit;">update</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">(</span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">id</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">)</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">{</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">.</span><span style="color:teal;font-size:inherit;font-family:inherit;font-weight:inherit;">ajax</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">({</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">url</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> : </span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"/api/Test?id="</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;">+</span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">id</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">,</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">type</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> : </span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"Put"</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">,</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">data</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> :</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">{</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"UserID"</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;">:</span><span style="color:#009999;font-size:inherit;font-family:inherit;font-weight:inherit;">1</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">,</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"UserName"</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;">:</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"moditest"</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">,</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"UserEmail"</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;">:</span><span style="color:#dd1144;font-size:inherit;font-family:inherit;font-weight:inherit;">"Parry@cnblogs.com"</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">},</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">success</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;">: </span><span style="color:#800080;font-size:inherit;font-family:inherit;font-weight:bold;">function</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">(</span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">data</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">)</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">{</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="font-size:inherit;font-family:inherit;font-weight:bold;">alert</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">(</span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">JSON</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">.</span><span style="color:teal;font-size:inherit;font-family:inherit;font-weight:inherit;">stringify</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">(</span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">data</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">));</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">}</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">});</span></span></p><p><span style="font-size:12px;"> </span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">}</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#800080;font-size:inherit;font-family:inherit;font-weight:bold;">function</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:teal;font-size:inherit;font-family:inherit;font-weight:inherit;">deletes</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">(</span><span style="color:#002d7a;font-size:inherit;font-family:inherit;font-weight:inherit;">id</span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">)</span><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">{</span></span></p><p><span style="font-size:12px;"><span style="color:#006fe0;font-size:inherit;font-family:inherit;font-weight:inherit;"> </span><span style="color:#333333;font-size:inherit;font-family:inherit;font-weight:inherit;">$.ajax({
url : “/api/Test/1”,
type : “DELETE”,
success: function (data) { alert(data);}
});
}
</script>
下面是ApiController部分的代码:
///
/// Test模块API
/// URI:/Api/Test
///
public class TestController : ApiController
{
///
/// User Data List
///
private readonly List _userList = new List
{
new Users {UserID = 1, UserName = “zzl”, UserEmail = “bfyxzls@sina.com”},
new Users {UserID = 2, UserName = “Spiderman”, UserEmail = “Spiderman@cnblogs.com”},
new Users {UserID = 3, UserName = “Batman”, UserEmail = “Batman@cnblogs.com”}
};
///
<