C# ASPNET

Command

code

Program


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddEndpointsApiExplorer(); // obligatoire
builder.Services.AddSwaggerGen();           // obligatoire


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
    app.UseSwagger();      // reconnu maintenant
    app.UseSwaggerUI();    // reconnu maintenant
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapGet("/", () => "API ASP.NET Core OK !");

app.MapControllers();

app.Run();
 

Controllers

AnimalController.cs



  using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebTuto2.Controllers
{

    public record AnimalDto(string Name, int Age);

    [ApiController]
    [Route("[controller]")]

    public class AnimalController : Controller
    {

        // si c'est le seul on peut laisser
        //  [HttpGet]


        [HttpGet("animals")]
        public IActionResult GetAnimals()
        {
            return Ok(new[] { "Chat", "Chien", "Oiseau" });
        }

        [HttpGet("homes")]
        public string[] GetHomes()
        {
            return new[] { "Home1", "Home2", "Home3" };
        }

        // ==============================================

        // -----------------------------
        // GET: api/animal/5
        // -----------------------------
        [HttpGet("{id:int}")]
        public IActionResult GetById(int id)
        {
            return Ok($"Animal with ID {id}");
        }

        // -----------------------------
        // GET: api/animal/search/Cat
        // -----------------------------
        [HttpGet("search/{name}")]
        public IActionResult Search(string name)
        {
            return Ok($"Recherche de {name}");
        }

        // -----------------------------
        // POST: api/animal
        // -----------------------------
        [HttpPost]
        public IActionResult Create([FromBody] AnimalDto dto)
        {
            Console.WriteLine($"Received animal: Name={dto.Name}, Age={dto.Age}");
            return CreatedAtAction(nameof(GetById), new { id = 1 }, dto);
        }

        // -----------------------------
        // PUT: api/animal/5
        // -----------------------------
        [HttpPut("{id:int}")]
        public IActionResult Update(int id, [FromBody] AnimalDto dto)
        {
            Console.WriteLine($"Updating animal {id} to: Name={dto.Name}, Age={dto.Age}");
            return Ok($"Animal {id} mis à jour");
        }

        // -----------------------------
        // PATCH: api/animal/5
        // -----------------------------
        [HttpPatch("{id:int}")]
        public IActionResult Patch(int id, [FromBody] object patchData)
        {
            return Ok($"Patch de l’animal {id}");
        }

        // -----------------------------
        // DELETE: api/animal/5
        // -----------------------------
        [HttpDelete("{id:int}")]
        public IActionResult Delete(int id)
        {
            return Ok($"Animal {id} supprimé");
        }

        // -----------------------------
        // HEAD: api/animal
        // -----------------------------
        [HttpHead]
        public IActionResult Head()
        {
            return Ok();
        }

        // -----------------------------
        // OPTIONS: api/animal
        // -----------------------------
        [HttpOptions]
        public IActionResult Options()
        {
            return Ok("GET, POST, PUT, DELETE, OPTIONS");
        }
    }
}

Properties/launchSettings.json


      {
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": false,
      "applicationUrl": "http://localhost:5196",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": false,
      "applicationUrl": "https://localhost:7191;http://localhost:5196",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}