Web API with ASP.NET Core 1.0 MVC

来源:互联网 发布:淘宝网购物怎么付款 编辑:程序博客网 时间:2024/06/05 23:39

1. Demo ASPI Overview :

APIDescriptionRequest bodyResponse bodyGET /api/todoGet all to-do itemsNoneArray of to-do itemsGET /api/todo/{id}Get an item by IDNoneTo-do itemPOST /api/todoAdd a new itemTo-do itemTo-do itemPUT /api/todo/{id}Update an existing item  To-do item DELETE /api/todo/{id}    Delete an item    None. No request body-None

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
原创粉丝点击