Two Web APIs in ASP.NET Core

ASP.NET Core has two ways to create RESTful Web API:

  • Based on Controller, interface endpoints are defined using a complete ControllerBase-based base class.
  • Based on Minimal APIs, interface endpoints are defined using Lambda expressions.

Controller-based Web APIs can use constructor injection, or property injection, following object-oriented patterns.
Web APIs based on Minimal APIs use injection through service providers.

Controller-based Web API example:

namespace APIWithControllers;
public class Program
{
    
    
    public static void Main(string[] args)
    {
    
    
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddControllers();
        var app = builder.Build();
        app.UseHttpsRedirection();
        app.MapControllers();
        app.Run();
    }
}
using Microsoft.AspNetCore.Mvc;

namespace APIWithControllers.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(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
    
    
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
    
    
            Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }
}

Examples of Web APIs based on Minimal APIs:

namespace MinimalAPI;

public class Program
{
    
    
    public static void Main(string[] args)
    {
    
    
        var builder = WebApplication.CreateBuilder(args);

        var app = builder.Build();

        app.UseHttpsRedirection();

        var summaries = new[]
        {
    
    
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        app.MapGet("/weatherforecast", (HttpContext httpContext) =>
        {
    
    
            var forecast = Enumerable.Range(1, 5).Select(index =>
                new WeatherForecast
                {
    
    
                    Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                    TemperatureC = Random.Shared.Next(-20, 55),
                    Summary = summaries[Random.Shared.Next(summaries.Length)]
                })
                .ToArray();
            return forecast;
        });

        app.Run();
    }
}

The same functionality can be achieved in two ways.
But there are some functions that Web API of Minimal APIs does not have, including:

  • No native support for model binding
  • No native support for authentication
  • Does not support application parts or application model
  • No native support for view rendering
  • JsonPatch is not supported
  • OData is not supported

Guess you like

Origin blog.csdn.net/cuit/article/details/132605788