Web API with ASP.NET Core 1.0 MVC
2016-12-09 11:55
856 查看
1. Demo ASPI Overview :
2. 创建API 项目:
实体类:
仓储类Repository:
注册仓储:(使用ASP.NET Core自带的依赖注入)
在Startup.cs中的ConfigureServices方法中:
添加控制器Web API Controller Class:
创建Action:CRUD
API | Description | Request body | Response body |
---|---|---|---|
GET /api/todo | Get all to-do items | None | Array of to-do items |
GET /api/todo/{id} | Get an item by ID | None | To-do item |
POST /api/todo | Add a new item | To-do item | To-do item |
PUT /api/todo/{id} | Update an existing item | To-do item | |
DELETE /api/todo/{id} | Delete an item | None. No request body- | None |
实体类:
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(); }
相关文章推荐
- 在ASP.NET Core MVC中构建简单 Web Api
- Getting started with ASP.NET Core MVC and Visual Studio
- Levenblog2.0.6 [With Asp.Net MVC 1.0]发布[build20090319]
- 在ASP.NET Core MVC中构建简单 Web Api
- ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0
- ASP.NET Core MVC中构建Web API
- Bare metal APIs with ASP.NET Core MVC(转)
- ASP.NET Core MVC中构建Web API
- [学习心得][Introduction to ASP.NET Core 1.0]3-2 ASP.NET Core and MVC Pattern
- Chapter 10: Test Driven Development with ASP.NET MVC --- Professional ASP.NET MVC 1.0
- 在ASP.NET Core MVC中构建简单 Web Api
- ASP.NET Core 1.0开发Web API程序
- LevenBlog2.0.7(With Asp.Net MVC1.0)发布-建议所有用户更新
- 从零开始学习 ASP.NET MVC 1.0 (四) View/Model 全解
- ASP.NET MVC 1.0正式发布
- 从零开始学习 ASP.NET MVC 1.0 (二) 识别URL的Routing组件
- ASP.NET MVC 1.0 发布了
- 从零开始学习ASP.NET MVC 1.0 (一) 开天辟地入门篇
- ASP.NET MVC 1.0 RC 那些事
- 【翻译】Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 4