« Asp.net core web api » : différence entre les versions
Ligne 18 : | Ligne 18 : | ||
public class ItemsController : ControllerBase | public class ItemsController : ControllerBase | ||
{ | { | ||
private readonly | private readonly IItemsRepository itemsRepository; | ||
private readonly IMapper mapper; | |||
public | public ItemsController(IItemsRepository itemsRepository, IMapper mapper) | ||
{ | { | ||
this.itemsRepository = itemsRepository; | |||
this.mapper = mapper; | |||
</filebox> | </filebox> | ||
Ligne 29 : | Ligne 30 : | ||
<kode lang='cs'> | <kode lang='cs'> | ||
[HttpGet] | [HttpGet] | ||
[ProducesResponseType( | [ProducesResponseType(typeof(IReadOnlyList<ItemDto>), StatusCodes.Status200OK)] | ||
public Task<IActionResult> GetAllAsync() | public async Task<IActionResult> GetAllAsync() | ||
{ | { | ||
var items = await itemsRepository.GetAllAsync() | var items = await itemsRepository.GetAllAsync() | ||
Ligne 38 : | Ligne 39 : | ||
[HttpGet("{id:int}")] | [HttpGet("{id:int}")] | ||
[ProducesResponseType(StatusCodes.Status200OK | [ProducesResponseType(typeof(ItemDto), StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status400BadRequest)] | |||
[ProducesResponseType(StatusCodes.Status404NotFound)] | [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
[ProducesResponseType(StatusCodes.Status500InternalServerError)] | [ProducesResponseType(StatusCodes.Status500InternalServerError)] | ||
Ligne 45 : | Ligne 47 : | ||
try | try | ||
{ | { | ||
if (id <= 0) | |||
{ | |||
return BadRequest(); | |||
} | |||
var item = await itemsRepository.GetByIdAsync(id) | var item = await itemsRepository.GetByIdAsync(id) | ||
if (item == null) | if (item == null) | ||
return NotFound(); | { | ||
return NotFound(); | |||
} | |||
return Ok( | var itemDto = mapper.Map<ItemDto>(item); | ||
return Ok(itemDto); | |||
} | } | ||
catch (Exception ex) | catch (Exception ex) | ||
Ligne 66 : | Ligne 72 : | ||
<kode lang='csharp'> | <kode lang='csharp'> | ||
[HttpPost] | [HttpPost] | ||
[ProducesResponseType( | [ProducesResponseType(typeof(ItemDto), StatusCodes.Status201Created)] | ||
public async Task<IActionResult> CreateAsync(ItemDto itemDto) | |||
public IActionResult | |||
{ | { | ||
var item = this.mapper.Map<Item>(itemDto); | |||
var createdItem = await itemsRepository.AddAsync(item); | |||
var createdItemDto = this.mapper.Map<ItemDto>(createdItem); | |||
return Created(HttpContext.Request.Path, createdItemDto); | |||
return Created($"https://localhost:5001/api/items/{createdItem.Id}", createdItemDto); | |||
return CreatedAtAction(nameof(Get), new { id = createdItem.Id }, createdItemDto); | |||
return Created($"https://localhost:5001/api/items/{ | |||
return CreatedAtAction(nameof(Get), new { id = | |||
return CreatedAtRoute("api", new { | return CreatedAtRoute("api", new { | ||
controller = "items", | controller = "items", | ||
action = nameof(Get), | action = nameof(Get), | ||
id = $"{ | id = $"{createdItem.Id}" | ||
}, | }, createdItemDto); | ||
} | } | ||
</kode> | </kode> | ||
<filebox fn='Startup.cs'> | <filebox fn='Startup.cs'> | ||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
Ligne 98 : | Ligne 100 : | ||
</filebox> | </filebox> | ||
== [https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api | == [https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api PUT / Update] == | ||
<kode lang='csharp'> | <kode lang='csharp'> | ||
[ProducesResponseType(StatusCodes.Status204NoContent)] | [ProducesResponseType(StatusCodes.Status204NoContent)] | ||
[ProducesResponseType(StatusCodes.Status400BadRequest)] | [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
public IActionResult | public async Task<IActionResult> UpdateAsync(int id, ItemDto itemDto) | ||
{ | { | ||
if (id <= 0) | |||
{ | |||
return BadRequest(); | |||
} | |||
var itemToUpdate = await itemsRepository.GetByIdAsync(id); | |||
if (itemToUpdate == null) | |||
{ | |||
return NotFound(); | |||
} | |||
var | var item = this.mapper.Map<Item>(itemDto); | ||
await itemsRepository.UpdateAsync(itemToUpdate, item); | |||
return NoContent(); | return NoContent(); | ||
} | } | ||
Ligne 126 : | Ligne 131 : | ||
[ProducesResponseType(StatusCodes.Status400BadRequest)] | [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
public IActionResult | public async Task<IActionResult> DeleteAsync(int id) | ||
{ | { | ||
if (id <= 0) | if (id <= 0) | ||
Ligne 141 : | Ligne 146 : | ||
} | } | ||
var itemToDelete = | var itemToDelete = await itemsRepository.GetByIdAsync(id); | ||
if (itemToDelete == null) | if (itemToDelete == null) | ||
return NotFound(); | return NotFound(); | ||
await itemsRepository.DeleteAsync(id); | |||
return NoContent(); | return NoContent(); | ||
} | } |
Version du 1 mai 2020 à 22:18
Liens
- Create web APIs with ASP.NET Core
- Build web APIs with ASP.NET CoreBuild web APIs with ASP.NET Core
- Controller action return types in ASP.NET Core Web API
- StatusCodes
- Path
Projet
Clique-droit sur le projet → Properties → Debug → décocher Launch browser
Controller
Controllers/ItemsController.cs |
[ApiController] [Produces("application/json")] // force le format JSON [ApiVersion("1")] // need nuget package Microsoft.AspNetCore.Mvc.Versioning [Route("api/[controller]")] // /api/items public class ItemsController : ControllerBase { private readonly IItemsRepository itemsRepository; private readonly IMapper mapper; public ItemsController(IItemsRepository itemsRepository, IMapper mapper) { this.itemsRepository = itemsRepository; this.mapper = mapper; |
GET
[HttpGet] [ProducesResponseType(typeof(IReadOnlyList<ItemDto>), StatusCodes.Status200OK)] public async Task<IActionResult> GetAllAsync() { var items = await itemsRepository.GetAllAsync() var itemDtos = mapper.Map<IReadOnlyList<ItemDto>>(items); return Ok(itemDtos); } [HttpGet("{id:int}")] [ProducesResponseType(typeof(ItemDto), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public IActionResult Get(int id) { try { if (id <= 0) { return BadRequest(); } var item = await itemsRepository.GetByIdAsync(id) if (item == null) { return NotFound(); } var itemDto = mapper.Map<ItemDto>(item); return Ok(itemDto); } catch (Exception ex) { // using Microsoft.AspNetCore.Http; return StatusCode(StatusCodes.Status500InternalServerError, $"Failed to get Item {id} ({ex})"); } } |
POST
[HttpPost] [ProducesResponseType(typeof(ItemDto), StatusCodes.Status201Created)] public async Task<IActionResult> CreateAsync(ItemDto itemDto) { var item = this.mapper.Map<Item>(itemDto); var createdItem = await itemsRepository.AddAsync(item); var createdItemDto = this.mapper.Map<ItemDto>(createdItem); return Created(HttpContext.Request.Path, createdItemDto); return Created($"https://localhost:5001/api/items/{createdItem.Id}", createdItemDto); return CreatedAtAction(nameof(Get), new { id = createdItem.Id }, createdItemDto); return CreatedAtRoute("api", new { controller = "items", action = nameof(Get), id = $"{createdItem.Id}" }, createdItemDto); } |
Startup.cs |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(routes => { routes.MapRoute( name: "api", template: "api/{controller=Items}/{id?}"); }); |
PUT / Update
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task<IActionResult> UpdateAsync(int id, ItemDto itemDto) { if (id <= 0) { return BadRequest(); } var itemToUpdate = await itemsRepository.GetByIdAsync(id); if (itemToUpdate == null) { return NotFound(); } var item = this.mapper.Map<Item>(itemDto); await itemsRepository.UpdateAsync(itemToUpdate, item); return NoContent(); } |
DELETE
[HttpDelete("{id:int}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task<IActionResult> DeleteAsync(int id) { if (id <= 0) { // do not provide details return BadRequest(); // return a string instead of ProblemDetails return BadRequest("id must be greater than 0."); // same as BadRequest() but with detail return Problem( title: "Bad Request", detail: "id must be greater than 0.", statusCode: StatusCodes.Status400BadRequest); } var itemToDelete = await itemsRepository.GetByIdAsync(id); if (itemToDelete == null) return NotFound(); await itemsRepository.DeleteAsync(id); return NoContent(); } |
Client
Client/ItemClient.cs |
public class ItemClient : IItemClient { private const string Path = "item"; private const string ApiVersion = "1"; private readonly HttpClient httpClient; private readonly QueryBuilder queryBuilder; // injection de HttpClient public ItemDataService(HttpClient httpClient) { this.httpClient = httpClient; queryBuilder = new QueryBuilder(); queryBuilder.Add("api-version", ApiVersion); } |
GET
ReadAsAsync
public async Task<IReadOnlyList<ItemDto>> GetAllItemsAsync(CancellationToken cancellationToken) { var uri = new Uri($"{Path}{queryBuilder.ToQueryString()}", UriKind.Relative); var response = await this.httpClient.GetAsync(uri, cancellationToken); response.EnsureSuccessStatusCode(); var items = await response.Content.ReadAsAsync<IReadOnlyList<ItemDto>>(cancellationToken); return items; } |
JsonSerializer.DeserializeAsync
public async Task<IReadOnlyList<ItemDto>> GetAllItemsAsync() { return await JsonSerializer.DeserializeAsync<IEnumerable<Item>>( await _httpClient.GetStreamAsync("api/item"), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); var response = await _httpClient.GetAsync("api/item"); if (!response.IsSuccessStatusCode) throw new HttpRequestException(response.ReasonPhrase); return await JsonSerializer.DeserializeAsync<IEnumerable<Item>>( await response.Content.ReadAsStreamAsync(), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); } public async Task<Item> GetItemAsync(int itemId) { return await JsonSerializer.DeserializeAsync<Item>( await _httpClient.GetStreamAsync($"api/item/{itemId}"), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); var response = await _httpClient.GetAsync($"item/{itemId}"); if (!response.IsSuccessStatusCode) throw new HttpRequestException(response.ReasonPhrase); return await JsonSerializer.DeserializeAsync<Item>( await response.Content.ReadAsStreamAsync()); } |
POST
public async Task<Item> AddItemAsync(Item item) { var itemJson = new StringContent(JsonSerializer.Serialize(item), Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync("api/item", itemJson); if (response.IsSuccessStatusCode) { return await JsonSerializer.DeserializeAsync<Item>(await response.Content.ReadAsStreamAsync()); } return null; } |
PUT
public async Task UpdateItemAsync(Item item) { var itemJson = new StringContent(JsonSerializer.Serialize(item), Encoding.UTF8, "application/json"); var response = await _httpClient.PutAsync("api/item", itemJson); if (!response.IsSuccessStatusCode) { var validationProblem = await JsonSerializer.DeserializeAsync<ValidationProblemDetails>( await response.Content.ReadAsStreamAsync()); // validationProblem.Errors: key = property, values = error list } } |
DELETE
public async Task DeleteItemAsync(int itemId) { var response = await _httpClient.DeleteAsync($"api/item/{itemId}"); if (!response.IsSuccessStatusCode) { var validationProblem = await JsonSerializer.DeserializeAsync<ValidationProblemDetails>( await response.Content.ReadAsStreamAsync()); throw new HttpRequestException($"{validationProblem.Title}: {validationProblem.Detail}"); } } |
API versioning
Need nuget package Microsoft.AspNetCore.Mvc.Versioning |
Controllers/ItemsController.cs |
[ApiVersion("1")] [ApiController] public class ItemsController : ControllerBase |
Startup.cs |
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddApiVersioning(); |
Url: /api/items?api-version=1 |
Validation
ItemViewModel.cs |
public class ItemViewModel { public int Id { get; set; } [Required] [MinLength(6)] public string Name { get; set; } } |
[HttpPost] public IActionResult Post([FromBody]ItemViewModel itemVm) { // ce code n'est plus nécessaire car pris en charge par les automatic HTTP 400 responses if (!ModelState.IsValid) { return BadRequest(ModelState); } var item = itemVm.ToEfItem(); _repository.Add(item); if (_repository.SaveAll()) { var newItemVm = new ItemViewModel(item); return Created($"/api/ListApi/{newItemVm.Id}", newItemVm); } else { return BadRequest("Failed to save."); } |
Automatic HTTP 400 responses
L'attribut [ApiController] effectue automatiquement une validation du modèle et envoie une réponse HTTP 400 en cas d'erreur.
Il n'est donc plus nécessaire de tester ModelState.IsValid.
ASP.NET Core MVC utilise le filtre d'action ModelStateInvalidFilter pour tester la validité du modèle.
Exemple de réponse en cas d'erreur de validation du modèle:
{ "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title": "One or more validation errors occurred.", "status": 400, "traceId": "|b659be6b-4fc34f5e9f3caf17.", "errors": { "Name": [ "The Name field is required." ] } } |
// déserialiser la réponse var content = await response.Content.ReadAsStreamAsync(); var validationProblem = await JsonSerializer.DeserializeAsync<ValidationProblemDetails>(content); |
Fluent Validation
dotnet add package FluentValidation.AspNetCore |
Startup.cs |
public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddFluentValidation(); services.AddTransient<IValidator<ItemDto>, ItemValidator>(); |
Validation/ItemValidator.cs |
public class ItemValidator : AbstractValidator<ItemDto> { public ItemValidator() { RuleFor(x => x.Name).NotEmpty(); |
Query Strings
Url: http://localhost:80/api/items?option1=true
ItemsController.cs |
[HttpGet] public IActionResult Get(bool option1 = false) // par défaut à false { } |
Images
Download
MyController.cs |
[HttpGet] public IActionResult Get() { var filePath = Path.Combine("~", "folder", "image.jpeg"); // VirtualFileResult return File(filePath, "image/jpeg"); } |
Base64
MyController.cs |
[HttpGet] public IActionResult Get() { var filePath = Path.Combine("~", "folder", "image.jpeg"); byte[] bytesArray = System.IO.File.ReadAllBytes(filePath); return Ok("data:image/jpeg;base64," + Convert.ToBase64String(bytesArray)); } |
Upload
MyController.cs |
[HttpPost] public async Task<IActionResult> Post([FromForm]IFormFile myFile) { var filePath = Path.Combine(folderPath, photo.FileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await myFile.CopyToAsync(stream); } return Ok(filePath); } |
Postman → Body → form-data
- key: myFile
- File
- Value: Choose Files
Si myFile est toujours null, enlever [FromForm] |
Renvoyer le message d'erreur des exceptions
Exception handler
Startup.cs |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { //app.UseDeveloperExceptionPage(); app.UseExceptionHandler("/error-local-development"); } else { app.UseExceptionHandler("/error"); } |
Controllers/ErrorController.cs |
[ApiController] public class ErrorController : ControllerBase { [Route("/error-local-development")] public IActionResult ErrorLocalDevelopment([FromServices] IWebHostEnvironment webHostEnvironment) { if (webHostEnvironment.EnvironmentName != "Development") { throw new InvalidOperationException("This shouldn't be invoked in non-development environments."); } var context = HttpContext.Features.Get<IExceptionHandlerFeature>(); return Problem( title: $"{context.Error.GetType().Name}: {context.Error.Message}", detail: context.Error.StackTrace); } [Route("/error")] public IActionResult Error() => Problem(); // "title": "An error occured while processing your request." public IActionResult Error() { var context = HttpContext.Features.Get<IExceptionHandlerFeature>(); return Problem(detail: $"{context.Error.GetType().Name}: {context.Error.Message}"); } } |
ExceptionFilterAttribute
ApiExceptionFilterAttribute.cs |
public class ApiExceptionFilterAttribute : ExceptionFilterAttribute { public override void OnException(ExceptionContext context) { context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.HttpContext.Response.ContentType = "text/plain"; context.HttpContext.Response.WriteAsync(context.Exception.Message).ConfigureAwait(false); } } |
MyController.cs |
[ApiExceptionFilter] [Route("api/[controller]")] public class MyController : Controller |
ExceptionMiddleware
ExceptionMiddleware.cs |
public class ExceptionMiddleware { public async Task Invoke(HttpContext context) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; var ex = context.Features.Get<IExceptionHandlerFeature>()?.Error; if (ex == null) return; context.Response.ContentType = "text/plain"; await context.Response.WriteAsync(ex.Message); } } |
Startup.cs |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseExceptionHandler(new ExceptionHandlerOptions { ExceptionHandler = new ExceptionMiddleware().Invoke }); app.UseMvc(); |
Différencier la gestion d'erreur pour mvc et webapi
Utiliser un ApiExceptionFilterAttribute et un MvcExceptionFilter
UseWhen et UseExceptionHandler
Startup.cs |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseWhen(x => x.Request.Path.Value.StartsWith("/api"), builder => { builder.UseExceptionHandler(new ExceptionHandlerOptions { ExceptionHandler = new ExceptionMiddleware().Invoke }); }); app.UseWhen(x => !x.Request.Path.Value.StartsWith("/api"), builder => { builder.UseExceptionHandler("/Home/Error"); }); |
UseExceptionHandler
ExceptionMiddleware.cs |
public class ExceptionMiddleware { public async Task Invoke(HttpContext context) { if (context.Request.Path.Value.StartsWith("/api")) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; var ex = context.Features.Get<IExceptionHandlerFeature>()?.Error; if (ex == null) return; context.Response.ContentType = "text/plain"; await context.Response.WriteAsync(ex.Message).ConfigureAwait(false); } else { // redirect to /Home/Error } } } |
UseStatusCodePagesWithReExecute
Startup.cs |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseStatusCodePagesWithReExecute("/error/{0}"); app.UseExceptionHandler("/error/500"); app.UseMvc(); |
[HttpGet("/error/{code:int}")] public string Error(int code) { return $"Error: {code}"; } |
Erreurs
Unsupported Media Type with Postman
- Body → raw = json à envoyer
- Headers → Content-Type = application/json
JsonSerializationException: Self referencing loop detected for property 'xxx' ...
Startup.cs |
services.AddMvc() // ignorer les références circulaires entre objets dans EF pour JSON .AddJsonOptions(opt => opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore); |
SqlException: Cannot insert explicit value for identity column in table 'xxx' when IDENTITY_INSERT is set to OFF
Impossible de sauvegarder les changements si une entité a été insérée avec une valeur ≠ default dans une propriété bindée avec une colonne Identity.
Solution: forcer la valeur de cette propriété a default. La bdd mettra la valeur de la propriété a jour après avoir inséré l'entité.