写在前面
文章整理了angularjs学习目录,有园子里的朋友问我要这方面的demo,周末也没什么事,就在之前的单页应用的demo上面添加了增删改查的操作。代码比较简单,这里只列举比较重要的代码片段。完整的代码将在文章下面提供链接。
demo
List lstOrders = = = ,);
response.Headers.Add(,); 方法,一般是GET,POST,PUT,DELETE,OPTIONS
response.Headers.Add(,);
lstOrders = List分页用
( i = ; i < ; i++ Product { Dt = DateTime.Now,Id = i + ,Name = + i.ToString(),Price = i +
<span style="color: #0000ff;">public <span style="color: #0000ff;">async Task Get(<span style="color: #0000ff;">string key,<span style="color: #0000ff;">int<span style="color: #000000;"> page)
{
<span style="color: #0000ff;">return <span style="color: #0000ff;">await Task.Factory.StartNew(() =><span style="color: #000000;">
{
<span style="color: #0000ff;">int pageSize = <span style="color: #800080;">10 <span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"> GET: api/Order</span>
<span style="color: #000000;"> [HttpGet]
[Route(
<span style="color: #0000ff;">public <span style="color: #0000ff;">async Task
{
<span style="color: #0000ff;">return <span style="color: #0000ff;">await Task.Factory.StartNew(() =><span style="color: #000000;">
{
<span style="color: #0000ff;">int pageSize = <span style="color: #800080;">10
</span><span style="color: #0000ff;">var</span> results = <span style="color: #0000ff;">string</span>.IsNullOrEmpty(key) ? lstOrders : lstOrders.Where(x =><span style="color: #000000;"> x.Name.Contains(key));
</span><span style="color: #008000;">//</span><span style="color: #008000;"><a href="/tag/fenye/" target="_blank" class="keywords">分页</a></span>
<span style="color: #0000ff;">var</span> pagedResults = results.Skip((page - <span style="color: #800080;">1</span>) *<span style="color: #000000;"> pageSize).Take(pageSize);
response.StatusCode </span>=<span style="color: #000000;"> HttpStatusCode.OK;
response.Content </span>= <span style="color: #0000ff;">new</span> StringContent(JsonConvert.SerializeObject(<span style="color: #0000ff;">new</span> { _code = <span style="color: #800080;">200</span>,_data =<span style="color: #000000;"> pagedResults }));
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> response;
});
}
[HttpPost]
[Route(</span><span style="color: #800000;">"</span><span style="color: #800000;">api/product/lists</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">async</span> Task<HttpResponseMessage><span style="color: #000000;"> Post([FromBody] Product order)
{
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">await</span> Task.Factory.StartNew(() =><span style="color: #000000;">
{
</span><span style="color: #008000;">//</span><span style="color: #008000;">模拟id</span>
order.Id = <span style="color: #0000ff;">new</span> Random().Next(<span style="color: #800080;">101</span>,<span style="color: #800080;">5000</span><span style="color: #000000;">);
lstOrders.Add(order);
response.StatusCode </span>=<span style="color: #000000;"> HttpStatusCode.OK;
response.Content </span>= <span style="color: #0000ff;">new</span> StringContent(JsonConvert.SerializeObject(<span style="color: #0000ff;">new</span> { _code = <span style="color: #800080;">200</span>,_data =<span style="color: #000000;"> order }));
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> response;
});
}
[HttpPut]
[Route(</span><span style="color: #800000;">"</span><span style="color: #800000;">api/product/lists</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
</span><span style="color: #008000;">//</span><span style="color: #008000;"> POST: api/Order</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">async</span> Task<HttpResponseMessage><span style="color: #000000;"> Put([FromBody] Product order)
{
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">await</span> Task.Factory.StartNew(() =><span style="color: #000000;">
{
</span><span style="color: #0000ff;">var</span> find = lstOrders.Find(o => o.Id ==<span style="color: #000000;"> order.Id);
</span><span style="color: #0000ff;">if</span> (find != <span style="color: #0000ff;">null</span><span style="color: #000000;">)
{
find.Name </span>=<span style="color: #000000;"> order.Name;
find.Price </span>=<span style="color: #000000;"> order.Price;
find.Dt </span>=<span style="color: #000000;"> order.Dt;
response.StatusCode </span>=<span style="color: #000000;"> HttpStatusCode.OK;
response.Content </span>= <span style="color: #0000ff;">new</span> StringContent(JsonConvert.SerializeObject(<span style="color: #0000ff;">new</span> { _code = <span style="color: #800080;">200</span>,_data =<span style="color: #000000;"> find }));
}
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> response;
});
}
[HttpDelete]
[Route(</span><span style="color: #800000;">"</span><span style="color: #800000;">api/product/lists</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
</span><span style="color: #008000;">//</span><span style="color: #008000;"> DELETE: api/Order/5</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">async</span> Task<HttpResponseMessage> Delete(<span style="color: #0000ff;">int</span><span style="color: #000000;"> id)
{
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">await</span> Task.Factory.StartNew(() =><span style="color: #000000;">
{
Product findOder </span>= lstOrders.Find(o => o.Id ==<span style="color: #000000;"> id);
lstOrders.Remove(findOder);
response.StatusCode </span>=<span style="color: #000000;"> HttpStatusCode.OK;
response.Content </span>= <span style="color: #0000ff;">new</span> StringContent(JsonConvert.SerializeObject(<span style="color: #0000ff;">new</span> { _code = <span style="color: #800080;">200</span>,_data =<span style="color: #000000;"> lstOrders }));
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> response;
});
}
}</span></pre>