Web API with ASP.NET Core 1.0 MVC
来源:互联网 发布:淘宝网购物怎么付款 编辑:程序博客网 时间:2024/06/05 23:39
1. Demo ASPI Overview :
API Description Request body Response body
2. 创建API 项目:
实体类:
namespace TodoApi.Models{ public class TodoItem { public string Key { get; set; } public string Name { get; set; } public bool IsComplete { get; set; } }}
仓储类Repository:
using System.Collections.Generic;namespace TodoApi.Models{ public interface ITodoRepository { void Add(TodoItem item); IEnumerable<TodoItem> GetAll(); TodoItem Find(string key); TodoItem Remove(string key); void Update(TodoItem item); }}
namespace TodoApi.Models{ public class TodoRepository : ITodoRepository { private static ConcurrentDictionary<string, TodoItem> _todos = new ConcurrentDictionary<string, TodoItem>(); public TodoRepository() { Add(new TodoItem { Name = "Item1" }); } public IEnumerable<TodoItem> GetAll() { return _todos.Values; } public void Add(TodoItem item) { item.Key = Guid.NewGuid().ToString(); _todos[item.Key] = item; } public TodoItem Find(string key) { TodoItem item; _todos.TryGetValue(key, out item); return item; } public TodoItem Remove(string key) { TodoItem item; _todos.TryRemove(key, out item); return item; } public void Update(TodoItem item) { _todos[item.Key] = item; } }}
注册仓储:(使用ASP.NET Core自带的依赖注入)
在Startup.cs中的ConfigureServices方法中:
public void ConfigureServices(IServiceCollection services){ // Add framework services. services.AddMvc(); services.AddSingleton<ITodoRepository, TodoRepository>();}
添加控制器Web API Controller Class:
namespace TodoApi.Controllers{ [Route("api/[controller]")] public class TodoController : Controller { public TodoController(ITodoRepository todoItems) { TodoItems = todoItems; } public ITodoRepository TodoItems { get; set; } }}
创建Action:CRUD
[HttpGet]public IEnumerable<TodoItem> GetAll(){ return TodoItems.GetAll();}[HttpGet("{id}", Name = "GetTodo")]public IActionResult GetById(string id){ var item = TodoItems.Find(id); if (item == null) { return NotFound(); } return new ObjectResult(item);}
[HttpPost]public IActionResult Create([FromBody] TodoItem item){ if (item == null) { return BadRequest(); } TodoItems.Add(item); return CreatedAtRoute("GetTodo", new { id = item.Key }, item);}
[HttpPut("{id}")]public IActionResult Update(string id, [FromBody] TodoItem item){ if (item == null || item.Key != id) { return BadRequest(); } var todo = TodoItems.Find(id); if (todo == null) { return NotFound(); } TodoItems.Update(item); return new NoContentResult();}
[HttpDelete("{id}")]public IActionResult Delete(string id){ var todo = TodoItems.Find(id); if (todo == null) { return NotFound(); } TodoItems.Remove(id); return new NoContentResult();}
0 0
- Web API with ASP.NET Core 1.0 MVC
- 在ASP.NET Core MVC中构建简单 Web Api
- ASP.NET Core MVC中构建Web API
- ASP.NET Core Web API 最小化项目
- 用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
- USING CORS WITH ASP NET WEB API
- 实战 ASP.NET MVC Web API
- ASP.NET MVC 4 (十二) Web API
- Asp.net MVC 与 Asp.net Web API 区别
- ASP.NET MVC与ASP.NET Web API的区别
- ASP.NET MVC与ASP.NET Web API的区别
- Authorizing your .NET Core MVC Core API requests with OpenIddict and Identity
- Docker容器环境下ASP.NET Core Web API
- ASP.NET Core Web API 描述文档-使用Swagger
- ASP.NET Core Web API get方法获取多个值
- 支持多个版本的ASP.NET Core Web API
- 在docker中运行ASP.NET Core Web API应用程序
- 在 Asp.net core 2.0 的Web Api 添加logging
- 冒泡及改进(java实现)
- 检测Android应用的启动与关闭
- 精通 CSS+DIV 网页样式与布局 93
- webservice调用方式
- laravel 表单和HTML扩展包
- Web API with ASP.NET Core 1.0 MVC
- 调试技巧之 找准调试点
- 精通 CSS+DIV 网页样式与布局 94
- java编程笔记
- Python列表
- EnhancedFor-增强for循环
- 浅谈MySQL索引背后的数据结构及算法
- 安卓硬件加速
- 简单HTTp几点