locked
dotnet-reactjs template - explain code sample RRS feed

  • Question

  • User1876496375 posted

    Hello, I just got started with using the dotnet-reactjs template on VS 2019. Can someone explain what these specific line of code do in the controller?

    I probably won't need to use any of this boilerplate code, but still would be helpful if I'm writing this on my own.

    Thanks!

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
            {
                _logger = logger;
            }

            [HttpGet]
            public IEnumerable<WeatherForecast> Get()
            { ... }

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace WebApplication1.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();
            }
        }
    }
    

    Monday, February 8, 2021 3:05 PM

All replies

  • User475983607 posted

    The code is a Web API controller.  You can learn about Web API from the official documentation. 

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio

    Monday, February 8, 2021 3:07 PM
  • User-1545767719 posted

    Hello, I just got started with using the dotnet-reactjs template on VS 2019. Can someone explain what these specific line of code do in the controller?

    Did you run your application and see the result displayed on your browser? You probably saw the followings:

    Welcome to your new single-page application, built with:

    • ASP.NET Core and C# for cross-platform server-side code
    • React for client-side code
    • Bootstrap for layout and styling

    The first one (i.e., ASP.NET Core and C# for cross-platform server-side code) is what you say "these specific line of code". It is a controller and action method of ASP.NET Core Web API.

    Click the "Fetch data" link shown at upper right side on the browser. The result obtained from the ASP.NET Core Web API will be shown on the browser.

    Tuesday, February 9, 2021 12:45 AM