索引:
一.创建一个空项目
请查看 新建 .NET Core 项目 -- Hello World! 一节,新建一个项目:
二.添加引用并修改配置为 Web API (.NET Core 已将 MVC/Web API 底层代码及表层编程接口合二为一)
修改 .vscode\launch.json 文件
代码如下:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/netcoreapp1.0/CoreTest.dll",
"args": [],
"cwd": "${workspaceRoot}",
"externalConsole": false,
"stopAtEntry": false,
"internalConsoleOptions": "openOnFirstSessionStart"
}
]
}
launch.json
修改 .vscode\tasks.json 文件
代码如下:
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"${workspaceRoot}\\project.json"
],
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
tasks.json
修改 project.json 项目文件
代码如下:
{
"version": "1.0.0-*", "buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
}, // 依赖项
"dependencies": {
"Microsoft.AspNetCore.Server.Kestrel":"1.0.0" ,
"Microsoft.AspNetCore.Mvc": "1.0.0"
}, "frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": [
"dnxcore50"
]
}
}
}
project.json
添加 Startup.cs 文件
代码如下:
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; namespace WebApiFrame
{
public class Startup
{
/*
* IServiceCollection 配置应用中可用的服务
*/
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
} /*
* IApplicationBuilder 构建HTTP请求管道
*/
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}
}
}
Startup.cs
修改 Program.cs 文件
代码如下:
using Microsoft.AspNetCore.Hosting; namespace WebApiFrame
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.Build(); host.Run();
}
}
}
Program.cs
三.添加 Hello World API
RESTFul 风格包含四种动词: GET/POST/PUT/DELETE
GET --> 获取
POST --> 更新
PUT --> 新建
DELETE --> 删除
添加 Controllers 文件夹
在此文件下添加 HelloWorldController.cs 文件
代码如下:
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc; namespace WebApiFrame.Controllers
{
[Route("api/[controller]")]
public class HelloWorldController : Controller
{
[HttpGet("{id}")]
public IActionResult Get(int id)
{
//
var model = new HelloWorldModel().All.FirstOrDefault(it => it.ID == id); //
var data = default(object);
if (model != null)
{
data = model;
}
else
{
data = new { Message = "没有数据!" };
} //
return Json(data);
} [HttpPost]
public IActionResult Post([FromBody] HelloWorldModel model)
{
return Json(new
{
Message = "更新成功!"
});
} [HttpPut]
public IActionResult Put([FromBody] HelloWorldModel model)
{
return Json(new
{
Message = "新建成功!"
});
} [HttpDelete("{id}")]
public IActionResult Delete(int id)
{
return Json(new
{
Message = id + ":删除成功!"
});
}
} public class HelloWorldModel
{
public int ID { get; set; }
public string PropA { get; set; }
public string PropB { get; set; } private static List<HelloWorldModel> _All = new List<HelloWorldModel>
{
new HelloWorldModel{ID=,PropA="1PA",PropB="1PB"},
new HelloWorldModel{ID=,PropA="2PA",PropB="2PB"},
new HelloWorldModel{ID=,PropA="3PA",PropB="3PB"}
};
public List<HelloWorldModel> All // 仅测试用,生产中这样写是有各种问题的,具体什么问题自己分析~~
{
get
{
return _All;
}
set
{
_All = value;
}
}
}
}
HelloWorldController.cs
四.使用 Visual Studio Code 运行
参照 新建 ASP.NET Core MVC 项目 -- Hello World! 调试运行,
使用 HTTP Post man 进行数据请求调试
GET : http://localhost:5000/api/HelloWorld/1 如图:
F5 调试通过,如图:
POST : http://localhost:5000/api/HelloWorld/
Content : 见下图
F5 调试通过,如图:
PUT: http://localhost:5000/api/HelloWorld/
Content : 见下图
F5 调试通过,如图:
DELETE : http://localhost:5000/api/HelloWorld/1
F5 调试通过,如图:
蒙
2016-09-22 11:58 周四