Weather Forecast Controller in ASP.net

  • by

By default we get this controller when we create our dotnet core app through the command line. As we have discussed before, the controller is the entry point to our application logic after the request has passed through the middleware. This is the place where we specify the paths and logic to be executed to our endpoints. The controllers contain a set of public methods that contain the logic to be executed based on the routing.These methods are known as action methods or actions. The controller class seems to inherit from the ControllerBase class, since it contains a set of predefined functionalities for building a web api.

{

   [ApiController]

   [Route(“[controller]”)]

   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;

       }

       [HttpGet]

       public IEnumerable<WeatherForecast> Get()

       {

           var rng = new Random();

           return Enumerable.Range(1, 5).Select(index => new WeatherForecast

           {

               Date = DateTime.Now.AddDays(index),

               TemperatureC = rng.Next(-20, 55),

               Summary = Summaries[rng.Next(Summaries.Length)]

           })

           .ToArray();

       }

   }

}

ApiController Attribute

In our weather forecast controller we have something called [ApiController], this is to annotate that this file is a controller. In dotnet core they call these annotations as attributes. Adding this attribute not only marks this class as a controller but also provides many functionalities that would be required for a web-api. Such as 

Route Attribute

Now that we have specified that the file is a controller using the ApiController, we need to specify application logic that needs to be executed for a particular path.There may be many controller files in the same application, but the request needs to be routed to one particular file that matches the path specified in the http request. This is done by matching the path specified in the http request with the one given as a constructor argument to this route attribute.These are known as templates. Here it is given as controller by default the path is taken as the name of the file, hence when the request is given as http://localhost:5000/weatherforecast, dotnet core routes the request to this file.

Check this link for more information on the Route attribute 

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.routeattribute?view=aspnetcore-5.0

Action Methods

Now the framework will be able to route the request to the correct file using the route attribute, the next step would be to find the action based on the request. Again, it is possible to have multiple public and private methods in the file from which the framework needs to select the method that we need. Here we have a method called Get. On top of that we have [HttpGet] as an attribute. This is to specify the framework to route the get requests to this method. Here we have only one method so by default the get request that we provide in the path http://localhost:5000/weatherforecast, gets automatically routed to this method. But when we have multiple public methods in the same file we provide the specific routing path as a constructor parameter to the [HttpGet]. The requests that we get in web-api are http type, hence it can be any http verb from the following,

Refer to the following link for a more detailed explanation about the routing mechanism. https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-5.0

Leave a Reply

Your email address will not be published.