Sunday 1 May 2022

Tutorial: Create a web API with ASP.NET Core

 https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio

  • Create a web API with controllers - TodoContext.cs added, Scaffold controller added
  • Create a minimal web API
  • Web API with MongoDB
  • Web API with JavaScript
  • Backend for mobile
  • Publish to Azure API Management

Create with controller

httprepl supports debugging.


After posting an item, you can see it.


After posting another item, you can see the Location header.

TodoItemsController.cs >> return CreatedAtAction() returns this response with Location header.


Prevent over-posting - interesting,  The subset of a model is usually referred to as a Data Transfer Object (DTO), input model, or view model.


When using httprepl, make sure you connect first. Otherwise PUT operation does not work.
PUT Success - 204 no content



PUT Error - 405 method not allowed

PUT Error - 400 bad request




Before DTO (Data Transfer Object)

Before DTO (Data Transfer Object)


How to update code to use DTO

        [HttpGet]
        public async Task<ActionResult<IEnumerable<TodoItemDTO>>> GetTodoItems()
        {
            //return await _context.TodoItems.ToListAsync();
            return await _context.TodoItems
                .Select(x => ItemToDTO(x))
                .ToListAsync();
        }


No comments:

Post a Comment