码迷,mamicode.com
首页 > Windows程序 > 详细

ASP.NET CORE WEB API DEMO 01

时间:2018-03-11 11:44:59      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:form   action   adr   ini   XML   request   enc   asp   service   

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

标签:form   action   adr   ini   XML   request   enc   asp   service   

原文地址:https://www.cnblogs.com/xiaowangzhi/p/8542968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!