使用Asp.net WebAPI 快速构建后台数据接口
2016-11-02 15:10
639 查看
现在的互联网应用,无论是web应用,还是移动APP,基本都需要实现非常多的数据访问接口。其实对一些轻应用来说Asp.net WebAPI是一个很快捷简单并且易于维护的后台数据接口框架。下面我们来快速构建一个基础数据操作接口。
新建项目
选择WebApi,并使用空模板(这里不想要一些其他的mvc的东西)
新建一个model
写几个属性
namespace WebApplication3.Models
{
public class Test
{
public int id
{ set; get; }
public string name
{ set; get; }
}
}
新增控制器
这里也用了空的控制器,避免多余代码干扰,其实后期可以写CodeSmith模板生成。
添加代码
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WebApplication3.Models;
namespace WebApplication3.Controllers
{
public class TestController : ApiController
{
Test[] products = new Test[]
{
new Test {
id = 1, name = "Tomato Soup"},
new Test {
id = 2, name = "Yo-yo" },
new Test {
id = 3, name = "Hammer" }
};
public IEnumerable<Test>
GetAllProducts()
{
return products;
}
public IHttpActionResult GetProduct(int id)
{
var product = products.FirstOrDefault((p) => p.id == id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
[HttpPost]
public IHttpActionResult PostTest([FromBody]Test t)
{
var product = products.FirstOrDefault((p) => p.id == t.id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}
}
运行页面
这里注意路由规则,api/控制器名称/id
也许你会说,我希望返回JSON格式的,好吧,增加下面两句。
其实就是修改Config的Formatter,使用JsonMediaTypeFormatter就好了。
你想问Post怎么调用?
当然也可以直接从Form中取值。例如:$("#SearchForm").serialize(),
能查询当然也能够进行增删改喽。
WebApi只有路由等基本框架,数据库操作完全可以自行选择,ADO.net, EF,nhibernate都可以。
果然是手机APP数据接口快速开发利器啊。
新建项目
选择WebApi,并使用空模板(这里不想要一些其他的mvc的东西)
新建一个model
写几个属性
namespace WebApplication3.Models
{
public class Test
{
public int id
{ set; get; }
public string name
{ set; get; }
}
}
新增控制器
这里也用了空的控制器,避免多余代码干扰,其实后期可以写CodeSmith模板生成。
添加代码
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WebApplication3.Models;
namespace WebApplication3.Controllers
{
public class TestController : ApiController
{
Test[] products = new Test[]
{
new Test {
id = 1, name = "Tomato Soup"},
new Test {
id = 2, name = "Yo-yo" },
new Test {
id = 3, name = "Hammer" }
};
public IEnumerable<Test>
GetAllProducts()
{
return products;
}
public IHttpActionResult GetProduct(int id)
{
var product = products.FirstOrDefault((p) => p.id == id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
[HttpPost]
public IHttpActionResult PostTest([FromBody]Test t)
{
var product = products.FirstOrDefault((p) => p.id == t.id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}
}
运行页面
这里注意路由规则,api/控制器名称/id
也许你会说,我希望返回JSON格式的,好吧,增加下面两句。
其实就是修改Config的Formatter,使用JsonMediaTypeFormatter就好了。
你想问Post怎么调用?
当然也可以直接从Form中取值。例如:$("#SearchForm").serialize(),
能查询当然也能够进行增删改喽。
WebApi只有路由等基本框架,数据库操作完全可以自行选择,ADO.net, EF,nhibernate都可以。
果然是手机APP数据接口快速开发利器啊。
相关文章推荐
- 使用Asp.net WebAPI 快速构建后台数据接口
- 使用ASP.NET 构建 Web 应用程序快速入门-8小时的免费培训视频 - Scott Hanselman的中文博客【转载】
- 使用 ASP.NET MVC 和 Ext JS 构建以数据为中心的 Web 应用程序
- 使用ASP.Net WebAPI构建REST服务(一)——简单的示例
- 使用ASP.Net WebAPI构建REST服务(二)——路由
- 使用ASP.Net WebAPI构建REST服务(六)——Self-Host
- 使用ASP.Net WebAPI构建REST服务(三)——返回值
- 利用ASP.NET MVC和Bootstrap快速搭建个人博客之后台dataTable数据列表
- 使用ASP.Net WebAPI构建REST服务——客户端
- ASP.NET中使用JSON方便实现前台与后台的数据交换
- 使用ASP.Net WebAPI构建REST服务(一)——简单的示例
- 关于“Asp.net 中后台CS读取数据库数据生成数组传递给前台页面JS使用”
- ASP.NET中使用开源组件NPOI快速导入导出Execl数据
- 使用ASP.Net WebAPI构建REST服务(七)——调试工具
- Extjs EditorGridPanel的使用.(数据从后台读出) asp.net
- ASP.NET中使用开源组件NPOI快速导入导出Execl数据
- 利用ASP.NET MVC和Bootstrap快速搭建个人博客之后台dataTable数据列表
- 使用ASP.Net WebAPI构建REST服务(四)——参数绑定
- Extjs和Asp.NET后台的数据交互(一) => Newtonsoft.Json 使用简介
- 使用ASP.Net WebAPI构建REST服务(五)——客户端