ASP.NET CORE WEB API DEMO 01

Posted xiaowangzhi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET CORE WEB API DEMO 01相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace Demo.Models
{
    public class BookChapter
    {
        public Guid Id { get; set; }
        public int Number { get; set; }
        public string Title { get; set; }
        public int Pages { get; set; }
    }

    public interface IBookChaptersRepository
    {
        void Init();
        IEnumerable<BookChapter> GetAll();
        BookChapter Find(Guid id);
        void Add(BookChapter chapter);
        BookChapter Remove(Guid id);
        void Update(BookChapter chapter);
    }

    public class BookChaptersRepository : IBookChaptersRepository
    {
        private readonly ConcurrentDictionary<Guid, BookChapter> _chapters =
            new ConcurrentDictionary<Guid, BookChapter>();

        public void Init()
        {
            Add(new BookChapter { Number = 1, Title = "Application Architectures", Pages = 35 });
            Add(new BookChapter { Number = 2, Title = "Core C#", Pages = 42 });
        }

        public IEnumerable<BookChapter> GetAll() => _chapters.Values;

        public BookChapter Find(Guid id)
        {
            BookChapter chapter;
            _chapters.TryGetValue(id, out chapter);
            return chapter;
        }

        public void Add(BookChapter chapter)
        {
            chapter.Id = Guid.NewGuid();
            _chapters[chapter.Id] = chapter;
        }

        public BookChapter Remove(Guid id)
        {
            BookChapter chapter;
            _chapters.TryRemove(id, out chapter);
            return chapter;
        }

        public void Update(BookChapter chapter)
        {
            _chapters[chapter.Id] = chapter;
        }
    }
}
using System;
using System.Collections.Generic;
using Demo.Models;
using Microsoft.AspNetCore.Mvc;

namespace Demo.Controllers
{
    [Produces("application/json", ("application/xml"))]
    [Route("api/[controller]")]
    public class BookChaptersController : Controller
    {
        private readonly IBookChaptersRepository _repository;
        public BookChaptersController(IBookChaptersRepository bookChaptersRepository)
        {
            _repository = bookChaptersRepository;
        }

        // GET api/bookchapters
        [HttpGet]
        public IEnumerable<BookChapter> GetBookChapters() => _repository.GetAll();

        // GET api/bookchapters/guid
        [HttpGet("{id}", Name = nameof(GetBookChapterById))]
        public IActionResult GetBookChapterById(Guid id)
        {
            BookChapter chapter = _repository.Find(id);
            if (chapter == null)
            {
                return NotFound();
            }
            return new ObjectResult(chapter);
        }

        // POST api/bookchapters
        [HttpPost]
        public IActionResult PostBookChapter([FromBody]BookChapter chapter)
        {
            if (chapter == null)
            {
                return BadRequest();
            }
            _repository.Add(chapter);
            return CreatedAtRoute(nameof(GetBookChapterById), new { id = chapter.Id }, chapter);
        }

        // PUT api/bookchapters
        [HttpPut("{id}")]
        public IActionResult PutBookChapter(Guid id, [FromBody]BookChapter chapter)
        {
            if (chapter == null || id != chapter.Id)
            {
                return BadRequest();
            }
            if (_repository.Find(id) == null)
            {
                return NotFound();
            }
            _repository.Update(chapter);
            return new NoContentResult();
        }

        // DELETE api/bookchapters/id
        [HttpDelete("id")]
        public void Delete(Guid id)
        {
            _repository.Remove(id);
        }
    }
}
using Demo.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Demo
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddXmlSerializerFormatters();
            IBookChaptersRepository repos = new BookChaptersRepository();
            repos.Init();
            services.AddSingleton(repos);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }
}

 

以上是关于ASP.NET CORE WEB API DEMO 01的主要内容,如果未能解决你的问题,请参考以下文章

将文件从 ASP.NET Core Web api 发布到另一个 ASP.NET Core Web api

ASP.NET Core Web Api 自动帮助页面

ASP.NET Core Web API

初试ASP.NET Web API/MVC API(附Demo)

Asp.Net Core 1.1 消费web api

使用 ASP.NET Core MVC 创建 Web API