Building RESTful APIs with C#

Your first web API with ASP.NET Core

Posted by Rodrigo Castro on December 27, 2024

ASP.NET Core makes building RESTful web APIs easy!

🚀 Getting Started

  1. Create a new project:
    1
    2
    
    dotnet new webapi -n MyApi
    cd MyApi
    
  2. Run your API:
    1
    
    dotnet run
    

    Visit https://localhost:5001/swagger to see it in action.

🏗️ Example: Simple Controller

1
2
3
4
5
6
7
[ApiController]
[Route("[controller]")]
public class HelloController : ControllerBase
{
    [HttpGet]
    public string Get() => "Hello, world!";
}
  • Add this to your Controllers folder.

📝 Key Concepts

  • Controllers: Handle HTTP requests (GET, POST, etc.)
  • Routing: URL patterns map to methods
  • Swagger: Built-in API documentation/testing

💡 Try It!

  • Add a GET /hello endpoint that returns a greeting.

Next: Creating and consuming REST APIs!