├── README.md
├── appsettings.Development.json
├── appsettings.json
├── BookAPI.csproj
├── BookAPI.csproj.user
├── Models
├── Book.cs
└── BookContext.cs
├── Repositories
├── IBookRepository.cs
└── BookRepository.cs
├── Program.cs
├── Properties
└── launchSettings.json
├── BookAPI.sln
├── Controllers
└── BooksController.cs
└── Startup.cs
/README.md:
--------------------------------------------------------------------------------
1 | # Web API with ASP.NET Core
2 | Source code for YouTube tutorial on how to create a Web API with ASP.NET Core.
3 | The API exposes endpoints for creating, reading, updating or deleting books.
4 |
--------------------------------------------------------------------------------
/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "AllowedHosts": "*"
10 | }
11 |
--------------------------------------------------------------------------------
/BookAPI.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net5.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/BookAPI.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ApiControllerEmptyScaffolder
5 | root/Common/Api
6 |
7 |
--------------------------------------------------------------------------------
/Models/Book.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 |
6 | namespace BookAPI.Models
7 | {
8 | public class Book
9 | {
10 | public int Id { get; set; }
11 | public string Title { get; set; }
12 | public string Author { get; set; }
13 | public string Description { get; set; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Repositories/IBookRepository.cs:
--------------------------------------------------------------------------------
1 | using BookAPI.Models;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 |
7 | namespace BookAPI.Repositories
8 | {
9 | public interface IBookRepository
10 | {
11 | Task> Get();
12 | Task Get(int id);
13 | Task Create(Book book);
14 | Task Update(Book book);
15 | Task Delete(int id);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Models/BookContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 |
7 | namespace BookAPI.Models
8 | {
9 | public class BookContext : DbContext
10 | {
11 | public BookContext(DbContextOptions options)
12 | :base(options)
13 | {
14 | Database.EnsureCreated();
15 | }
16 | public DbSet Books { get; set; }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Hosting;
2 | using Microsoft.Extensions.Configuration;
3 | using Microsoft.Extensions.Hosting;
4 | using Microsoft.Extensions.Logging;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Threading.Tasks;
9 |
10 | namespace BookAPI
11 | {
12 | public class Program
13 | {
14 | public static void Main(string[] args)
15 | {
16 | CreateHostBuilder(args).Build().Run();
17 | }
18 |
19 | public static IHostBuilder CreateHostBuilder(string[] args) =>
20 | Host.CreateDefaultBuilder(args)
21 | .ConfigureWebHostDefaults(webBuilder =>
22 | {
23 | webBuilder.UseStartup();
24 | });
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json.schemastore.org/launchsettings.json",
3 | "iisSettings": {
4 | "windowsAuthentication": false,
5 | "anonymousAuthentication": true,
6 | "iisExpress": {
7 | "applicationUrl": "http://localhost:52236",
8 | "sslPort": 44375
9 | }
10 | },
11 | "profiles": {
12 | "IIS Express": {
13 | "commandName": "IISExpress",
14 | "launchBrowser": true,
15 | "launchUrl": "swagger",
16 | "environmentVariables": {
17 | "ASPNETCORE_ENVIRONMENT": "Development"
18 | }
19 | },
20 | "BookAPI": {
21 | "commandName": "Project",
22 | "dotnetRunMessages": "true",
23 | "launchBrowser": true,
24 | "launchUrl": "swagger",
25 | "applicationUrl": "https://localhost:5001;http://localhost:5000",
26 | "environmentVariables": {
27 | "ASPNETCORE_ENVIRONMENT": "Development"
28 | }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/BookAPI.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30717.126
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BookAPI", "BookAPI.csproj", "{4689AFAD-93AD-4AF0-910E-CD808F116562}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {4689AFAD-93AD-4AF0-910E-CD808F116562}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {4689AFAD-93AD-4AF0-910E-CD808F116562}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {4689AFAD-93AD-4AF0-910E-CD808F116562}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {4689AFAD-93AD-4AF0-910E-CD808F116562}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {6313064E-3690-42D7-8658-5CF53B71DC46}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Repositories/BookRepository.cs:
--------------------------------------------------------------------------------
1 | using BookAPI.Models;
2 | using Microsoft.EntityFrameworkCore;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 |
8 | namespace BookAPI.Repositories
9 | {
10 | public class BookRepository : IBookRepository
11 | {
12 | private readonly BookContext _context;
13 |
14 | public BookRepository(BookContext context)
15 | {
16 | _context = context;
17 | }
18 |
19 | public async Task Create(Book book)
20 | {
21 | _context.Books.Add(book);
22 | await _context.SaveChangesAsync();
23 |
24 | return book;
25 | }
26 |
27 | public async Task Delete(int id)
28 | {
29 | var bookToDelete = await _context.Books.FindAsync(id);
30 | _context.Books.Remove(bookToDelete);
31 | await _context.SaveChangesAsync();
32 | }
33 |
34 | public async Task> Get()
35 | {
36 | return await _context.Books.ToListAsync();
37 | }
38 |
39 | public async Task Get(int id)
40 | {
41 | return await _context.Books.FindAsync(id);
42 | }
43 |
44 | public async Task Update(Book book)
45 | {
46 | _context.Entry(book).State = EntityState.Modified;
47 | await _context.SaveChangesAsync();
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Controllers/BooksController.cs:
--------------------------------------------------------------------------------
1 | using BookAPI.Models;
2 | using BookAPI.Repositories;
3 | using Microsoft.AspNetCore.Http;
4 | using Microsoft.AspNetCore.Mvc;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Linq;
8 | using System.Threading.Tasks;
9 |
10 | namespace BookAPI.Controllers
11 | {
12 | [Route("api/[controller]")]
13 | [ApiController]
14 | public class BooksController : ControllerBase
15 | {
16 | private readonly IBookRepository _bookRepository;
17 |
18 | public BooksController(IBookRepository bookRepository)
19 | {
20 | _bookRepository = bookRepository;
21 | }
22 |
23 | [HttpGet]
24 | public async Task> GetBooks()
25 | {
26 | return await _bookRepository.Get();
27 | }
28 |
29 | [HttpGet("{id}")]
30 | public async Task> GetBooks(int id)
31 | {
32 | return await _bookRepository.Get(id);
33 | }
34 |
35 | [HttpPost]
36 | public async Task>PostBooks([FromBody] Book book)
37 | {
38 | var newBook = await _bookRepository.Create(book);
39 | return CreatedAtAction(nameof(GetBooks), new { id = newBook.Id }, newBook);
40 | }
41 |
42 | [HttpPut]
43 | public async Task PutBooks(int id, [FromBody] Book book)
44 | {
45 | if(id != book.Id)
46 | {
47 | return BadRequest();
48 | }
49 |
50 | await _bookRepository.Update(book);
51 |
52 | return NoContent();
53 | }
54 |
55 | [HttpDelete("{id}")]
56 | public async Task Delete (int id)
57 | {
58 | var bookToDelete = await _bookRepository.Get(id);
59 | if (bookToDelete == null)
60 | return NotFound();
61 |
62 | await _bookRepository.Delete(bookToDelete.Id);
63 | return NoContent();
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/Startup.cs:
--------------------------------------------------------------------------------
1 | using BookAPI.Models;
2 | using BookAPI.Repositories;
3 | using Microsoft.AspNetCore.Builder;
4 | using Microsoft.AspNetCore.Hosting;
5 | using Microsoft.AspNetCore.HttpsPolicy;
6 | using Microsoft.AspNetCore.Mvc;
7 | using Microsoft.EntityFrameworkCore;
8 | using Microsoft.Extensions.Configuration;
9 | using Microsoft.Extensions.DependencyInjection;
10 | using Microsoft.Extensions.Hosting;
11 | using Microsoft.Extensions.Logging;
12 | using Microsoft.OpenApi.Models;
13 | using System;
14 | using System.Collections.Generic;
15 | using System.Linq;
16 | using System.Threading.Tasks;
17 |
18 | namespace BookAPI
19 | {
20 | public class Startup
21 | {
22 | public Startup(IConfiguration configuration)
23 | {
24 | Configuration = configuration;
25 | }
26 |
27 | public IConfiguration Configuration { get; }
28 |
29 | // This method gets called by the runtime. Use this method to add services to the container.
30 | public void ConfigureServices(IServiceCollection services)
31 | {
32 | services.AddScoped();
33 | services.AddDbContext(o => o.UseSqlite("Data source=books.db"));
34 | services.AddControllers();
35 | services.AddSwaggerGen(c =>
36 | {
37 | c.SwaggerDoc("v1", new OpenApiInfo { Title = "BookAPI", Version = "v1" });
38 | });
39 | }
40 |
41 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
42 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
43 | {
44 | if (env.IsDevelopment())
45 | {
46 | app.UseDeveloperExceptionPage();
47 | app.UseSwagger();
48 | app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BookAPI v1"));
49 | }
50 |
51 | app.UseHttpsRedirection();
52 |
53 | app.UseRouting();
54 |
55 | app.UseAuthorization();
56 |
57 | app.UseEndpoints(endpoints =>
58 | {
59 | endpoints.MapControllers();
60 | });
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------