Overview of controllers 

Controllers are a fundamental part of the web API in ASP.NET Core projects. They handle incoming requests and act as the entry point of our application. We'll look at controllers in more detail in Chapter 4, Dependency Injection, but for now, let's examine the default WeatherForecastController provided by the web API template:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace SampleAPI.Controllers
{
[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();
}
}
}

WeatherForecastController comes with basic methods. By default, it doesn't use any data source; it simply returns some mock values. Let's proceed by having a look at the main elements of the WeatherForecastController class:

  • The ApiController attribute indicates that the controller and all extended controllers serve HTTP API responses. It was introduced in ASP.NET Core version 2.1, and it is usually combined with the ControllerBase class.
  • The Route("api/[controller]") attribute defines the route for our controller. For example, in this case the controller will respond to the following URI: https://myhostname:myport/api/weatherforecast. The [controller] placeholder is used to indicate the name of the controller. 
  • The ControllerBase class is usually combined with the ApiController attribute, and it is defined in the Microsoft.AspNetCore.Mvc namespace. The ControllerBase class indicates a controller without support for the views part. It provides a few base methods, such as Created, CreatedAtAction, and NotFound. It also provides some properties, for example, HttpContext, which contains the requests and responses of our web service.
  • The HttpGet attribute is part of the Microsoft.AspNetCore.Mvc namespace. It identifies the type of HTTP method accepted by the action. It also accepts an additional parameter, such as [HttpGet("{id}")], which defines the route template of the action. ASP.NET Core exposes an HTTP attribute for each HTTP verb, such as HttpPost, HttpPut, or HttpDelete.

Finally, we can take a brief look at the implementation of the WeatherForecastController() constructor method and the Get() method. The first initializes all dependencies of the controller class, and it is the dependency injection entry point for our class; all dependencies relating to the controllers are solved in the constructor. The Get() method implements the logic and returns a collection of elements that will be serialized and then passed to the HTTP response of the web API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.121.214