A modern, lightweight weather forecast API built with ASP.NET Core and .NET 9, providing simulated weather data for demonstration purposes.
This project is a sample API that demonstrates best practices for creating RESTful services using ASP.NET Core minimal APIs with OpenAPI/Swagger documentation. It provides a single endpoint that returns a 5-day weather forecast with randomly generated temperatures and weather conditions.
- Built on .NET 9 using ASP.NET Core minimal APIs
- OpenAPI/Swagger documentation with custom configuration
- CORS support for cross-origin requests
- HTTP and HTTPS support
- Clean, maintainable code structure
- Example of record types in C#
- .NET 9 SDK or later
- Visual Studio Code (optional)
- C# Dev Kit extension for VS Code (optional)
Using the .NET CLI:
# Clone the repository
git clone https://github.com/yourusername/weather-forecast.git
cd weather-forecast
# Run the application
dotnet run
Using Visual Studio Code:
- Open the project folder in Visual Studio Code
- Make sure you have the C# Dev Kit extension installed
- Press F5 or use the Run and Debug view to start the application
- The Swagger UI will automatically open in your browser
The API provides the following endpoint:
- GET /api/weatherforecast
- Returns a 5-day weather forecast
- No authentication required
- Response: Array of
WeatherForecast
objects
When running in development mode, Swagger UI is available at:
The raw OpenAPI specification is available at:
- HTTP: http://localhost:5046/swagger/v1/swagger.json
- HTTPS: https://localhost:7082/swagger/v1/swagger.json
The API returns WeatherForecast
objects with the following properties:
Property | Type | Description |
---|---|---|
date | string (date) | The date of the forecast |
temperatureC | integer | Temperature in Celsius |
summary | string | Weather condition description (e.g., "Freezing", "Bracing", "Chilly", etc.) |
temperatureF | integer | Temperature in Fahrenheit (calculated from Celsius) |
Example response:
[
{
"date": "2023-10-15",
"temperatureC": 14,
"summary": "Bracing",
"temperatureF": 57
},
{
"date": "2023-10-16",
"temperatureC": 7,
"summary": "Cool",
"temperatureF": 44
}
]
The application can be configured through the appsettings.json
file and environment variables. Key configuration areas include:
- HTTP/HTTPS Endpoints: Configured in
Properties/launchSettings.json
- CORS Policy: Configured in
Program.cs
- Swagger/OpenAPI: Customized in
Program.cs
You can test the API using:
- Swagger UI: Available at
/swagger
when running in development mode - curl:
curl -X GET "https://localhost:7082/api/weatherforecast" -H "accept: application/json"
- Postman or similar API testing tools
- REST Client extension for VS Code
- ASP.NET Core documentation
- Minimal APIs in .NET
- OpenAPI and Swagger in ASP.NET Core
- Working with .NET in VS Code
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or support, please contact:
- Email: api@contoso.com
- Website: https://contoso.com/support
This README is for a sample project and may need modifications to match your specific implementation details.