Sobes.tech
Junior — Middle

Have you ever developed a REST API using C#?

sobes.tech AI

Answer from AI

Yes, experience in developing REST APIs in C# typically involves using ASP.NET Core. In this framework, controllers are created to handle HTTP requests and return data in JSON or XML format.

Example of a simple REST API controller in C#:

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private readonly IProductService _service;

    public ProductsController(IProductService service)
    {
        _service = service;
    }

    [HttpGet]
    public IActionResult GetAll()
    {
        var products = _service.GetAllProducts();
        return Ok(products);
    }

    [HttpGet("{id}")]
    public IActionResult GetById(int id)
    {
        var product = _service.GetProductById(id);
        if (product == null)
            return NotFound();
        return Ok(product);
    }

    [HttpPost]
    public IActionResult Create(Product product)
    {
        _service.AddProduct(product);
        return CreatedAtAction(nameof(GetById), new { id = product.Id }, product);
    }
}

This approach makes it easy to create scalable and maintainable APIs.