From 6ad3430b8f5f6db11b43ec787da9a187162ffc62 Mon Sep 17 00:00:00 2001 From: Pierre Malarme Date: Fri, 16 Sep 2022 11:28:53 +0200 Subject: [PATCH 1/2] Update to run with .Net 6 --- .gitignore | 3 +++ Controllers/TodoController.cs | 39 ++++++++++++++--------------- Models/TodoContext.cs | 4 +-- Models/TodoItem.cs | 2 +- Properties/launchSettings.json | 25 +++++++++++++++---- Startup.cs | 40 ++++++++++-------------------- TodoApi.csproj | 18 +++++++------- appsettings.Development.json | 3 +-- appsettings.json | 2 +- wwwroot/app/scripts/app.js | 2 +- wwwroot/app/scripts/todoListSvc.js | 12 ++++----- 11 files changed, 76 insertions(+), 74 deletions(-) diff --git a/.gitignore b/.gitignore index 940794e6..bd1cfc91 100644 --- a/.gitignore +++ b/.gitignore @@ -286,3 +286,6 @@ __pycache__/ *.btm.cs *.odx.cs *.xsd.cs + +# VS Code +.vscode \ No newline at end of file diff --git a/Controllers/TodoController.cs b/Controllers/TodoController.cs index aad8137b..f65ae526 100644 --- a/Controllers/TodoController.cs +++ b/Controllers/TodoController.cs @@ -1,37 +1,28 @@ using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.EntityFrameworkCore; -using Newtonsoft.Json; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Threading.Tasks; using TodoApi.Models; namespace TodoApi.Controllers { - [Route("api/[controller]")] + [Route("api/[controller]")] [ApiController] - public class TodoController : Controller + public class TodoController : ControllerBase { private readonly TodoContext _context; public TodoController(TodoContext context) { _context = context; - - if (_context.TodoItems.Count() == 0) - { - _context.TodoItems.Add(new TodoItem { Name = "Item1" }); - _context.SaveChanges(); - } } // GET: api/Todo [HttpGet] - public async Task>> GetTodoItem() + public async Task>> GetTodoItems() { + if (_context.TodoItems == null) + { + return NotFound(); + } return await _context.TodoItems.ToListAsync(); } @@ -39,6 +30,10 @@ public async Task>> GetTodoItem() [HttpGet("{id}")] public async Task> GetTodoItem(long id) { + if (_context.TodoItems == null) + { + return NotFound(); + } var todoItem = await _context.TodoItems.FindAsync(id); if (todoItem == null) @@ -90,13 +85,17 @@ public async Task> PostTodoItem(TodoItem todoItem) _context.TodoItems.Add(todoItem); await _context.SaveChangesAsync(); - return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem); + return CreatedAtAction(nameof(GetTodoItem), new { id = todoItem.Id }, todoItem); } // DELETE: api/Todo/5 [HttpDelete("{id}")] - public async Task> DeleteTodoItem(long id) + public async Task DeleteTodoItem(long id) { + if (_context.TodoItems == null) + { + return NotFound(); + } var todoItem = await _context.TodoItems.FindAsync(id); if (todoItem == null) { @@ -106,12 +105,12 @@ public async Task> DeleteTodoItem(long id) _context.TodoItems.Remove(todoItem); await _context.SaveChangesAsync(); - return todoItem; + return NoContent(); } private bool TodoItemExists(long id) { - return _context.TodoItems.Any(e => e.Id == id); + return (_context.TodoItems?.Any(e => e.Id == id)).GetValueOrDefault(false); } } } diff --git a/Models/TodoContext.cs b/Models/TodoContext.cs index de24b36a..b6bb8b4f 100644 --- a/Models/TodoContext.cs +++ b/Models/TodoContext.cs @@ -4,11 +4,11 @@ namespace TodoApi.Models { public class TodoContext : DbContext { - public TodoContext (DbContextOptions options) + public TodoContext(DbContextOptions options) : base(options) { } - public DbSet TodoItems { get; set; } + public DbSet TodoItems { get; set; } = null!; } } diff --git a/Models/TodoItem.cs b/Models/TodoItem.cs index d3eef6e3..cc533de4 100644 --- a/Models/TodoItem.cs +++ b/Models/TodoItem.cs @@ -3,7 +3,7 @@ public class TodoItem { public long Id { get; set; } - public string Name { get; set; } + public string? Name { get; set; } public bool IsComplete { get; set; } } } \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index ae124698..fa064787 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -1,16 +1,31 @@ -{ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, - "anonymousAuthentication": true + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:23810", + "sslPort": 44332 + } }, "profiles": { "TodoApi": { "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "api/todo", + "applicationUrl": "https://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", "launchBrowser": true, + "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "http://localhost:5000" + } } } -} \ No newline at end of file +} diff --git a/Startup.cs b/Startup.cs index aeb859af..ade55762 100644 --- a/Startup.cs +++ b/Startup.cs @@ -1,9 +1,4 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; using TodoApi.Models; @@ -26,40 +21,31 @@ public void ConfigureServices(IServiceCollection services) // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { - c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); + c.SwaggerDoc("v1", new OpenApiInfo { Title = "TodoApi", Version = "v1" }); + }); + services.AddDbContext(options => + { + options.UseInMemoryDatabase("TodoList"); }); - - services.AddDbContext(options => options.UseInMemoryDatabase("TodoList")); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment environment) { + if (environment.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(); - // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. - app.UseSwaggerUI(c => - { - c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); - }); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - //app.UseHttpsRedirection(); - + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TodoApi v1")); app.UseDefaultFiles(); - app.UseStaticFiles(); - + app.UseHttpsRedirection(); app.UseRouting(); - app.UseAuthorization(); - app.UseEndpoints(endpoints => { endpoints.MapControllers(); diff --git a/TodoApi.csproj b/TodoApi.csproj index 4696ca57..777d71b7 100644 --- a/TodoApi.csproj +++ b/TodoApi.csproj @@ -1,20 +1,20 @@ - netcoreapp3.1 + net6.0 + enable + enable - - - - + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - + + + + + diff --git a/appsettings.Development.json b/appsettings.Development.json index c9294ca4..b0bacf42 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -2,8 +2,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Microsoft.AspNetCore": "Warning" } } } diff --git a/appsettings.json b/appsettings.json index 84563e54..93b64d37 100644 --- a/appsettings.json +++ b/appsettings.json @@ -7,4 +7,4 @@ } }, "AllowedHosts": "*" -} \ No newline at end of file +} diff --git a/wwwroot/app/scripts/app.js b/wwwroot/app/scripts/app.js index f68f3bf9..5be2ca05 100644 --- a/wwwroot/app/scripts/app.js +++ b/wwwroot/app/scripts/app.js @@ -13,4 +13,4 @@ angular.module('todoApp', ['ngRoute']) templateUrl: "/App/Views/TodoList.html", }).otherwise({ redirectTo: "/Home" }); - }]); \ No newline at end of file + }]); diff --git a/wwwroot/app/scripts/todoListSvc.js b/wwwroot/app/scripts/todoListSvc.js index 55a18d60..280787ca 100644 --- a/wwwroot/app/scripts/todoListSvc.js +++ b/wwwroot/app/scripts/todoListSvc.js @@ -7,22 +7,22 @@ angular.module('todoApp') return { getItems : function(){ - return $http.get(apiEndpoint + '/api/Todo'); + return $http.get(apiEndpoint + '/api/todo'); }, getItem : function(id){ - return $http.get(apiEndpoint + '/api/Todo/' + id); + return $http.get(apiEndpoint + '/api/todo/' + id); }, postItem : function(item){ - return $http.post(apiEndpoint + '/api/Todo', item); + return $http.post(apiEndpoint + '/api/todo', item); }, putItem : function(item){ - return $http.put(apiEndpoint + '/api/Todo/' + item.id, item); + return $http.put(apiEndpoint + '/api/todo/' + item.id, item); }, deleteItem : function(id){ return $http({ method: 'DELETE', - url: apiEndpoint + '/api/Todo/' + id + url: apiEndpoint + '/api/todo/' + id }); } }; -}]); \ No newline at end of file +}]); From 3e575bdf84e5184d8a0cd6c63002d34f778d8534 Mon Sep 17 00:00:00 2001 From: Pierre Malarme Date: Tue, 20 Sep 2022 10:17:42 +0200 Subject: [PATCH 2/2] Update TodoController from ControllerBase to Controller --- Controllers/TodoController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controllers/TodoController.cs b/Controllers/TodoController.cs index f65ae526..a749ff88 100644 --- a/Controllers/TodoController.cs +++ b/Controllers/TodoController.cs @@ -6,7 +6,7 @@ namespace TodoApi.Controllers { [Route("api/[controller]")] [ApiController] - public class TodoController : ControllerBase + public class TodoController : Controller { private readonly TodoContext _context;