ASP.NET Core Web API Basics Explained with Example

ASP.NET Core Web API allows developers to build RESTful services that can be consumed by web, mobile, or desktop applications. It uses controllers, routing, and JSON serialization to handle HTTP requests.

Key Features:

  • Supports HTTP methods: GET, POST, PUT, DELETE
  • Returns data in JSON or XML format
  • Integrates with routing and model binding
  • Supports dependency injection and middleware
  • Ideal for creating RESTful services

Example – Controller:

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private static readonly List<string> products = new() { "Laptop", "Mobile" };

    [HttpGet]
    public IActionResult Get() => Ok(products);

    [HttpPost]
    public IActionResult Post([FromBody] string product)
    {
        products.Add(product);
        return CreatedAtAction(nameof(Get), new { }, product);
    }
}

Accessing API endpoints:

  • GET https://localhost:5001/api/products
  • POST with JSON body { "product": "Tablet" }

ASP.NET Core Web API enables building scalable, maintainable, and cross-platform services for modern applications.

Citations:

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *