[译]Create a Web API in MVC 6
原文: http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6
ASP.NET 5.0的一个目标是合并MVC和Web API frameworkd.
创建空白ASP.NET 5项目
打开Visual Studio 2015. 在File菜单, 选择 New > Project.
在New Project对话框中, 点击Templates > Visual C# > Web, 选择ASP.NET Web Application项目模板. 起名为 "TodoApi" .

在New ASP.NET Project对话框中, 选择"ASP.NET 5.0 Empty"模板.

下面的图片显示了项目的结构.

项目包括下面的文件:
- global.json包含解决方案级别的设置, 包含了项目之间的依赖关系.
- project.json包含项目的配置.
- Project_Readme.html是一个readme文件.
- Startup.cs 包含了startup和配置代码.
Startup类定义在Startup.cs文件中, 配置了ASP.NET请求管道. 当你使用了空白的项目模板, Startup class 基本上不包含什么代码添加请求管道:
public class Startup
{
public void Configure(IApplicationBuilder app)
{
// Nothing here!
}
}
现在就可以运行程序了, 但是他不包含任何功能. 如果使用"Starter Web" 模板,会自己默认就配置了一些框架,例如MVC 6, Entity Framework, authentication, logging等.
添加欢迎页面
打开project.json文件. 这个文件包含了项目的一些配置. dependencies 部分列出了项目需要的NuGet包和类库. 添加Microsoft.AspNet.Diagnostics包:
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-beta1",
// Add this:
"Microsoft.AspNet.Diagnostics": "1.0.0-beta1"
},
当你在输入的时候, Visual Studio的智能提示会列出一些NuGet包.

智能提示也会提供包的版本号:

接下来, 打开Startup.cs添加下面的代码.
using System;
using Microsoft.AspNet.Builder; namespace TodoApi
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
// New code
app.UseWelcomePage();
}
}
}
按F5后你会看到类似于下面的一个欢迎页面:

创建Web API
创建一个web API管理ToDo条目. 首先添加ASP.NET MVC 6.
在project.json的dependencies的section添加MVC 6包:
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-beta1",
"Microsoft.AspNet.Diagnostics": "1.0.0-beta1",
// New:
"Microsoft.AspNet.Mvc": "6.0.0-beta1"
},
下一步添加MVC请求管道. 在Startup.cs中,
- 添加
usingstatement语句. usingMicrosoft.Framework.DependencyInjection - 在
Startup类中添加下面的方法.public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}上面的代码添加了所有MVC6需要的依赖. 项目启动的时候会自动调用
ConfigureServices. - 在Configure方法中, 添加下面的代码.
UseMvc方法添加MVC 6到请求管道.public void Configure(IApplicationBuilder app)
{
// New:
app.UseMvc();
}
下面是完整的Startup类:
using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
// New using:
using Microsoft.Framework.DependencyInjection; namespace TodoApi
{
public class Startup
{
// Add this method:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
} public void Configure(IApplicationBuilder app)
{
// New:
app.UseMvc();
app.UseWelcomePage();
}
}
}
添加模型
using System.ComponentModel.DataAnnotations; namespace TodoApi.Models
{
public class TodoItem
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
public bool IsDone { get; set; }
}
}
添加控制器
using Microsoft.AspNet.Mvc;
using System.Collections.Generic;
using System.Linq;
using TodoApi.Models; namespace TodoApi.Controllers
{ [Route("api/[controller]")]
public class TodoController : Controller
{
static readonly List<TodoItem> _items = new List<TodoItem>()
{
new TodoItem { Id = 1, Title = "First Item" }
}; [HttpGet]
public IEnumerable<TodoItem> GetAll()
{
return _items;
} [HttpGet("{id:int}", Name = "GetByIdRoute")]
public IActionResult GetById (int id)
{
var item = _items.FirstOrDefault(x => x.Id == id);
if (item == null)
{
return HttpNotFound();
} return new ObjectResult(item);
} [HttpPost]
public void CreateTodoItem([FromBody] TodoItem item)
{
if (!ModelState.IsValid)
{
Context.Response.StatusCode = 400;
}
else
{
item.Id = 1+ _items.Max(x => (int?)x.Id) ?? 0;
_items.Add(item); string url = Url.RouteUrl("GetByIdRoute", new { id = item.Id },
Request.Scheme, Request.Host.ToUriComponent()); Context.Response.StatusCode = 201;
Context.Response.Headers["Location"] = url;
}
} [HttpDelete("{id}")]
public IActionResult DeleteItem(int id)
{
var item = _items.FirstOrDefault(x => x.Id == id);
if (item == null)
{
return HttpNotFound();
}
_items.Remove(item);
return new HttpStatusCodeResult(204); // 201 No Content
}
}
}
上面的控制器实行了基本的增删改查操作:
| Request (HTTP method + URL) | Description |
|---|---|
| GET /api/todo | Returns all ToDo items |
| GET /api/todo/id | Returns the ToDo item with the ID given in the URL. |
| POST /api/todo | Creates a new ToDo item. The client sends the ToDo item in the request body. |
| DELETE /api/todo/id | Deletes a ToDo item. |
请求:
GET http://localhost:5000/api/todo HTTP/1.1
User-Agent: Fiddler
Host: localhost:5000
响应:
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 30 Oct 2014 22:40:31 GMT
Content-Length: 46 [{"Id":1,"Title":"First Item","IsDone":false}]
解释代码
Routing路由
[Route] attribute 定义了这个控制器URL模板:
[Route("api/[controller]")]
在上面的例子中“[controller]” 替代controller的类名, 减去“Controller”后缀. api/todo匹配TodoController控制器.
HTTP 方法
[HttpGet], [HttpPost] and [HttpDelete] attributes 定义了action的Http方法
[HttpGet]
public IEnumerable<TodoItem> GetAll() {} [HttpGet("{id:int}", Name = "GetByIdRoute")]
public IActionResult GetById (int id) {} [HttpPost]
public void CreateTodoItem([FromBody] TodoItem item) {} [HttpDelete("{id:int}")]
public IActionResult DeleteItem(int id) {}
在上面的例子中 GetById 和 DeleteItem, 的定义了参数. 完整的路由模板是“api/[controller]/{id:int}”.
在 “{id:int}” 片段中, id是一个参数, and “:int”限制了参数的类型是整形. 下面的URL会被匹配:
http://localhost/api/todo/1
http://localhost/api/todo/42
但是下面的URL不会匹配:
http://localhost/api/todo/abc
注意GetById 和 DeleteItem 同样也有一个方法参数名为id. 例如, http://localhost/api/todo/42, id的值会被设置为42.
CreateTodoItem 方法展示了另外一种形式的参数绑定:
[HttpPost]
public void CreateTodoItem([FromBody] TodoItem item) {}
[FromBody] attribute告诉框架会将请求内容序列会为TodoItem参数.
下面展示了请求URL和匹配的对应的控制器方法:
| Request | Controller Action |
|---|---|
| GET /api/todo | GetAll |
| POST /api/todo | CreateTodoItem |
| GET /api/todo/1 | GetById |
| DELETE /api/todo/1 | DeleteItem |
| GET /api/todo/abc | none – returns 404 |
| PUT /api/todo | none – returns 404 |
Action的返回值
TodoController 展示了几种不同的返回值.
GetAll方法返回一个CLR对象.
[HttpGet]
public IEnumerable<TodoItem> GetAll()
{
return _items;
}
默认返回值是JSON, 但是用户可以请求其它的格式. 例如, 下面请求一个XML响应.
GET http://localhost:5000/api/todo HTTP/1.1
User-Agent: Fiddler
Host: localhost:5000
Accept: application/xml
Response:
HTTP/1.1 200 OK
Content-Type: application/xml;charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 30 Oct 2014 22:40:10 GMT
Content-Length: 228 <ArrayOfTodoItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/TodoApi.Models"><TodoItem><Id>1</Id><IsDone>false</IsDone><Title>First Item</Title></TodoItem></ArrayOfTodoItem>
GetById方法返回IActionResult:
[HttpGet("{id:int}", Name = "GetByIdRoute")]
public IActionResult GetById (int id)
{
var item = _items.FirstOrDefault(x => x.Id == id);
if (item == null)
{
return HttpNotFound();
}
return new ObjectResult(item);
}
如果没有找到对应的todo, 返回HttpNotFound.
最后, CreateTodoItem展示如何直接修改reponse的属性返回.
[HttpPost]
public void CreateTodoItem([FromBody] TodoItem item)
{
// (some code not shown here) Context.Response.StatusCode = 201;
Context.Response.Headers["Location"] = url;
}
依赖注入
MVC 6直接把依赖注入集成在了框架中:
using System.Collections.Generic; namespace TodoApi.Models
{
public interface ITodoRepository
{
IEnumerable<TodoItem> AllItems { get; }
void Add(TodoItem item);
TodoItem GetById(int id);
bool TryDelete(int id);
}
}
实现.
using System;
using System.Collections.Generic;
using System.Linq; namespace TodoApi.Models
{
public class TodoRepository : ITodoRepository
{
readonly List<TodoItem> _items = new List<TodoItem>(); public IEnumerable<TodoItem> AllItems
{
get
{
return _items;
}
} public TodoItem GetById(int id)
{
return _items.FirstOrDefault(x => x.Id == id);
} public void Add(TodoItem item)
{
item.Id = 1 + _items.Max(x => (int?)x.Id) ?? 0;
_items.Add(item);
} public bool TryDelete(int id)
{
var item = GetById(id);
if (item == null)
{
return false;
}
_items.Remove(item);
return true;
}
}
}
在控制器中使用构造函数依赖注入:
[Route("api/[controller]")]
public class TodoController : Controller
{
// Remove this code:
//static readonly List<TodoItem> _items = new List<TodoItem>()
//{
// new TodoItem { Id = 1, Title = "First Item" }
//};
// Add this code:
private readonly ITodoRepository _repository;
public TodoController(ITodoRepository repository)
{
_repository = repository;
}
更新代码使用仓储操作数据:
[HttpGet]
public IEnumerable<TodoItem> GetAll()
{
return _repository.AllItems;
}
[HttpGet("{id:int}", Name = "GetByIdRoute")]
public IActionResult GetById(int id)
{
var item = _repository.GetById(id);
if (item == null)
{
return HttpNotFound();
} return new ObjectResult(item);
} [HttpPost]
public void CreateTodoItem([FromBody] TodoItem item)
{
if (!ModelState.IsValid)
{
Context.Response.StatusCode = 400;
}
else
{
_repository.Add(item); string url = Url.RouteUrl("GetByIdRoute", new { id = item.Id }, Request.Scheme, Request.Host.ToUriComponent());
Context.Response.StatusCode = 201;
Context.Response.Headers["Location"] = url;
}
} [HttpDelete("{id}")]
public IActionResult DeleteItem(int id)
{
if (_repository.TryDelete(id))
{
return new HttpStatusCodeResult(204); // 201 No Content
}
else
{
return HttpNotFound();
}
}
为了依赖注入能正常的工作, 我们需要注册仓储到依赖注入系统. 在Startup类中, 添加下面的代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// New code
services.AddSingleton<ITodoRepository, TodoRepository>();
}
当应用运行的时候,框架会自动将TodoRepository注入到控制器. 因为我们使用AddSingleton注册ITodoRepository, 在整个应用程序的生命周期中会使用同一个实例.
[译]Create a Web API in MVC 6的更多相关文章
- 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】
Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...
- YbSoftwareFactory 代码生成插件【二十一】:Web Api及MVC性能提升的几个小技巧
最近在进行 YbSoftwareFactory 的流程功能升级,目前已经基本完成,现将用到的一些关于 Web Api 及 MVC 性能提升的一些小技巧进行了总结,这些技巧在使用.配置上也相当的简单,但 ...
- Entity Framework 6 Recipes 2nd Edition(9-3)译->找出Web API中发生了什么变化
9-3. 找出Web API中发生了什么变化 问题 想通过基于REST的Web API服务对数据库进行插入,删除和修改对象图,而不必为每个实体类编写单独的更新方法. 此外, 用EF6的Code Fri ...
- web api与mvc的区别
MVC主要用来构建网站,既关心数据也关心页面展示,而Web API只关注数据 Web API支持格式协商,客户端可以通过Accept header通知服务器期望的格式 Web API支持Self Ho ...
- Web API与MVC控制器的区别
Web API属于ASP.NET核心平台的一部分,它利用MVC框架的底层功能方便我们快速的开发部署WEB服务.我们可以在常规MVC应用通过添加API控制器来创建web api服务,普通MVC应用程序控 ...
- ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API
ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ASP.NET MVC 6 中创建简单的web API. 如何从空的项目模板中启 ...
- 在 MVC6 中创建 Web API
ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ...
- 【ASP.NET MVC 5】第27章 Web API与单页应用程序
注:<精通ASP.NET MVC 3框架>受到了出版社和广大读者的充分肯定,这让本人深感欣慰.目前该书的第4版不日即将出版,现在又已开始第5版的翻译,这里先贴出该书的最后一章译稿,仅供大家 ...
- Asp.Net Web API VS Asp.Net MVC
http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-W ...
随机推荐
- poj 1269 线段相交/平行
模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = #include <cmath> #include <cstdio> #include <iostrea ...
- Delicious Retouch 3
今天发现一个photoshop的插件:Delicious Retouch 3,磨皮的,特好用,各种磨皮方法的合集.今后都不敢说自己会磨皮了. 插件的界面 插件的使用教程 链接:http://pan.b ...
- Chrome控制台 JS调试的一些小技巧
$ $_命令返回最近一次表达式执行的结果,功能跟按向上的方向键再回车是一样的,但它可以做为一个变量使用在你接下来的表达式中. $0~$4则代表了最近5个你选择过的DOM节点.在页面右击选择审查元素,然 ...
- JavaWeb学习总结-02 Tomcat 学习和使用
一 Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...
- 【项目】'NSRangeException', reason: '*** -[__NSArrayM removeObjectAtIndex:]: index 2 beyond bounds [0 .. 1]'
问题代码: [self.assetsArray objectAtIndex:indexPath.row] 问题解决思路:这里
- rabbitmq因为主机名更改导致连接不上
今天创建rabbitmq用户的时候报错,说nodedown,定睛一看,发现主机名是localhost,而此时我们的主机名是controller01,所以猜测是主机名导致的,重启服务器,就成功添加了. ...
- python和numpy的版本、安装位置
命令行下查看python和numpy的版本和安装位置 1.查看python版本 方法一: python -V 注意:‘-V‘中‘V’为大写字母,只有一个‘-’ 方法二: python --versio ...
- matlab和C/C++混合编程--Mex
最近的项目需要matlab和C的混合编程,经过一番努力终于完成了项目要解决的问题.现在就将Mex的一些经验总结一下,当然只是刚刚开始,以后随着学习的深入继续添加.首先讲讲写Mex的一些常规规定,然后我 ...
- IIS------配置错误:不能在此路径中使用此配置节
转载: http://wenda.so.com/q/1414673956725716 因为 IIS 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改.运行命令行 %wi ...
- tp中附件上传文件,表单提交
public function tianjia(){ $goods=D('Goods'); if(!empty($_POST)){ if($_FILES['f_goods_image']['error ...