12/12/2022 4:05:58 AM

Change the default WeatherForecastController.cs to use a subfolder and add multiple actions. I created the subfolder "v1." While this is not technically needed (you can map the default controller), it helps to have your folder structure mirror the route prefix.

//Folder ///Controllers/v1/WeatherForecastController.cs using Microsoft.AspNetCore.Mvc; namespace MyWebsite.Controllers.v1 { [ApiController] //adding "v1/" to add a v1 prefix //adding /[action] to use the function names as endpoints [Route("v1/[controller]/[action]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; private readonly ILogger<WeatherForecastController> _logger; public WeatherForecastController(ILogger<WeatherForecastController> logger) { _logger = logger; } //remove this, code changed to return only 1 //[HttpGet(Name = "GetWeatherForecast")] //url: /v1/WeatherForecast/Get public WeatherForecast Get() { return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = Random.Shared.Next(-20, 55), Summary = Summaries[Random.Shared.Next(Summaries.Length)] }) .ToArray()[0]; } //url: /v1/WeatherForecast/List public IEnumerable<WeatherForecast> List() { return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = Random.Shared.Next(-20, 55), Summary = Summaries[Random.Shared.Next(Summaries.Length)] }) .ToArray(); } } }