使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

上一篇里,我已经建立了一个简单的Web-Demo应用程序。这一篇将记录将此Demo程序改造成一个Web Api应用程序。

一、添加ASP.NET Core MVC包

1. 在project.json文件添加Microsoft.AspNetCore.Mvc包

 1 {
 2   "version": "1.0.0-*",
 3   "buildOptions": {
 4     "debugType": "portable",
 5     "emitEntryPoint": true
 6   },
 7   "dependencies": {
 8     "Microsoft.NETCore.App": {
 9       "type": "platform",
10       "version": "1.0.0"
11     },
12     "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
13     "Microsoft.AspNetCore.Mvc": "1.0.0"
14   },
15   "frameworks": {
16     "netcoreapp1.0": {
17       "imports": "dnxcore50"
18     }
19   }
20 }

2. 在cmd窗口使用 dotnet restore 将新添加的包还原至本地

二、修改Startup.cs文件内容,添加Asp.Net Mvc能力

 1 using Microsoft.AspNetCore.Builder;
 2 using Microsoft.Extensions.DependencyInjection;
 3 
 4 namespace WebApiFrame
 5 {
 6     public class Startup
 7     {
 8         public void ConfigureServices(IServiceCollection services)
 9         {
10 
11             // 注入MVC框架
12             services.AddMvc();
13         }
14 
15         public void Configure(IApplicationBuilder app)
16         {
17             // 添加MVC中间件
18             app.UseMvc();
19         }
20     }
21 }

三、新建Controller,编写Web Api方法

1. 控制器UsersController

 1 using System;
 2 using Microsoft.AspNetCore.Mvc;
 3 using WebApiFrame.Models;
 4 
 5 namespace WebApiFrame.Controller
 6 {
 7 
 8     [Route("api/[controller]")]
 9     public class UsersController : Microsoft.AspNetCore.Mvc.Controller
10     {
11 
12         [HttpGet("{id}")]
13         public IActionResult Get(int id)
14         {
15             var user = new User() { Id = id, Name = "Name:" + id, Sex = "Male" };
16             return new ObjectResult(user);
17         }
18 
19         [HttpPost]
20         public IActionResult Post([FromBody] User user){
21             if(user == null){
22                 return BadRequest();
23             }
24 
25             // TODO:新增操作
26             user.Id = new Random().Next(1, 10);
27             return CreatedAtAction("Get", new { id = user.Id }, user);
28         }
29 
30         [HttpPut("{id}")]
31         public IActionResult Put(int id, [FromBody] User user){
32             if(user == null){
33                 return BadRequest();
34             }
35 
36             // TODO: 更新操作
37             return new NoContentResult();
38         }
39 
40         [HttpDelete("{id}")]
41         public void Delete(int id){
42             // TODO: 删除操作
43             
44         }
45     }
46 }

不同于之前的ASP.NET MVC版本,ASP.NET Core MVC里实现Web Api的控制器都继承自唯一的一个基类Controller。

2. 模型User.cs

1 namespace WebApiFrame.Models
2 {
3     public class User
4     {
5         public int Id { get; set; }
6         public string Name { get; set; }
7         public string Sex { get; set; }
8     }
9 }

最终文件夹结构如下图

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

四、启动调试,使用Fiddler工具调试Web Api接口

1. GET Request

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

GET Response

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

2. POST Request

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

POST Response

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

POST响应码为201,表示资源创建成功。

在响应头里有一个Location属性,这是一个导航属性,属性值是一个url地址,直接指向了刚刚Post成功的资源地址。

3. PUT Request

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

PUT Response

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

PUT为更新操作。按照规范,当服务更新操作执行成功后,直接通过响应码204告诉客户端调用成功,默认没有响应body。

4. DELETE Request

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

DELETE Response

使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo

DELETE为删除操作。按照规范,需要通过响应码判断是否成功(200)还是失败(500),默认没有响应body。