[译]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中,
- 添加
using
statement语句. 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 ...
随机推荐
- Android成长日记-WebView使用
在App中有时候会看到一些页面是以网页的形式展示,其原理就是运用了WebView,下面予以讲述WebView 1. 使用Intent调用系统浏览器或者第三方浏览器打开网页 调用系统浏览器打开页面 Ur ...
- Nginx 日志分享
Nginx 日志对于大部分人来说是个未被发掘的宝藏,总结之前做某日志分析系统的经验,和大家分享一下 Nginx 日志的纯手工分析方式. Nginx 日志相关配置有 2 个地方:access_log 和 ...
- PHP中的一个”坑“
说一个极有可能在工作中遇到的问题——foreach的引用 foreach $arr = range(1,3); //[1,2,3] foreach($arr as &$val) { } for ...
- Windows8远程桌面CentOS 6.5
概述: 在CentOS上安装vncserver和Windows8上安装VNC Viewer,从而可以在Windows8远程桌面到CentOS 6.5 1. 在CentOS上安装vncserver 1. ...
- 数据结构作业——word(栈)
Description TonyY 是一个 word 小白,今天他对 word 中撤销和恢复功能特别感兴趣,玩耍了一个上午(mdzz~) ,现在他知道了它们的功能和快捷键:撤销:ctrl+z,可以撤销 ...
- HTML5学习总结-07 WebStorage 本地存储
一 Storage sessionStorage session临时回话,从页面打开到页面关闭的时间段 窗口的临时存储,页面关闭,本地存储消失 localStorage 永久存储(可以手动删除数据)S ...
- C++ Virtual
摘自:http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html namespace QCAR { /// Area is the ...
- Yocto开发笔记之《网卡配置》(QQ交流群:519230208)
QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ============================================== # ifconfig -a # ...
- ServiceStack.Text 更快的序列化
Json.net 是以前最经常用的序列化组件,后来又注意到ServiceStack号称最快的,所以我做了以下测试 1)Json.net using System; using System.Colle ...
- JSP 原理
参考文献:http://www.cnblogs.com/xdp-gacl/p/3764991.html 一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都 ...