Язык программирования C#9 и платформа .NET5 - Троелсен Эндрю
На нашем сайте KnigaRead.com Вы можете абсолютно бесплатно читать книгу онлайн Троелсен Эндрю, "Язык программирования C#9 и платформа .NET5" бесплатно, без регистрации.
/// <response code="200">Found and deleted the record</response>/// <response code="400">Bad request</response>[Produces("application/json")][ProducesResponseType(StatusCodes.Status200OK)][ProducesResponseType(StatusCodes.Status400BadRequest)][SwaggerResponse(200, "The execution was successful")][SwaggerResponse(400, "The request was invalid")][HttpDelete("{id}")]public ActionResult<T> DeleteOne(int id, T entity){ if (id != entity.Id) { return BadRequest(); } try { MainRepo.Delete(entity); } catch (Exception ex) { // Должно обрабатываться более элегантно. return new BadRequestObjectResult(ex.GetBaseException()?.Message); } return Ok();}Метод начинается с определения маршрута как запроса
HttpDeleteidididНа этом создание базового контроллера завершено.
Класс CarsController
Приложению
AutoLot.ApiHttpGetCarMakeCarsControllerControllersCarsControllerusingusing System.Collections.Generic;using AutoLot.Api.Controllers.Base;using Microsoft.AspNetCore.Mvc;using AutoLot.Models.Entities;using AutoLot.Dal.Repos.Interfaces;using AutoLot.Services.Logging;using Microsoft.AspNetCore.Http;using Swashbuckle.AspNetCore.Annotations;Класс
CarsControllerBaseCrudControllernamespace AutoLot.Api.Controllers{ [Route("api/[controller]")] public class CarsController : BaseCrudController<Car, CarsController> { public CarsController(ICarRepo carRepo, IAppLogging<CarsController> logger) : base(carRepo, logger) { } }}Класс
CarsController/// <summary>/// Gets all cars by make/// </summary>/// <returns>All cars for a make</returns>/// <param name="id">Primary key of the make</param>/// <response code="200">Returns all cars by make</response>[Produces("application/json")][ProducesResponseType(StatusCodes.Status200OK)][ProducesResponseType(StatusCodes.Status204NoContent)][SwaggerResponse(200, "The execution was successful")][SwaggerResponse(204, "No content")][HttpGet("bymake/{id?}")]public ActionResult<IEnumerable<Car>> GetCarsByMake(int? id){ if (id.HasValue && id.Value>0) { return Ok(((ICarRepo)MainRepo).GetAllBy(id.Value)); } return Ok(MainRepo.GetAllIgnoreQueryFilters());}Атрибут
HttpGetbymakehttps://localhost:5021/api/cars/bymake/5 Сначала в методе проверяется, было ли передано значение для
idGetAllBy()CarRepoMainRepoIRepo<T>ICarRepo