< Summary

Information
Class: ProjectTemplate.Web.Controllers.Api.ApplicationInformationResponse
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Controllers/Api/ApplicationInformationController.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 80
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Controllers/Api/ApplicationInformationController.cs

#LineLine coverage
 1using Asp.Versioning;
 2using Microsoft.AspNetCore.Mvc;
 3using Microsoft.Extensions.Options;
 4using ProjectTemplate.Web.Options;
 5
 6namespace ProjectTemplate.Web.Controllers.Api;
 7
 8/// <summary>
 9/// Provides sample versioned API endpoints for the application.
 10/// </summary>
 11[ApiController]
 12[ApiVersion("1.0")]
 13[ApiVersion("0.9", Deprecated = true)]
 14[Route("api/v{version:apiVersion}/application-information")]
 15[Route("api/application-information")]
 16public sealed class ApplicationInformationController(
 17    IOptions<ApplicationApiVersioningOptions> apiVersioningOptions) : ControllerBase
 18{
 19    private const string _deprecationHeaderName = "Deprecation";
 20    private const string _sunsetHeaderName = "Sunset";
 21
 22    private readonly ApplicationApiVersioningOptions _apiVersioningOptions =
 23        apiVersioningOptions?.Value ?? throw new ArgumentNullException(nameof(apiVersioningOptions));
 24
 25    /// <summary>
 26    /// Returns application API information for the requested API version.
 27    /// </summary>
 28    /// <returns>Application API version information.</returns>
 29    [HttpGet]
 30    public ActionResult<ApplicationInformationResponse> Get()
 31    {
 32        ApiVersion requestedVersion = HttpContext.Features
 33            .Get<IApiVersioningFeature>()
 34            ?.RequestedApiVersion ?? new ApiVersion(1, 0);
 35
 36        if (requestedVersion.MajorVersion == 0 && requestedVersion.MinorVersion == 9)
 37        {
 38            AppendDeprecationHeaders();
 39        }
 40
 41        return Ok(new ApplicationInformationResponse(
 42            ApplicationName: "ProjectTemplate.Web",
 43            ApiVersion: FormatApiVersion(requestedVersion),
 44            Message: "API versioning foundation active."));
 45    }
 46
 47    private static string FormatApiVersion(ApiVersion version)
 48    {
 49        int minorVersion = version.MinorVersion ?? 0;
 50
 51        return string.Create(
 52            System.Globalization.CultureInfo.InvariantCulture,
 53            $"{version.MajorVersion}.{minorVersion}");
 54    }
 55
 56    private void AppendDeprecationHeaders()
 57    {
 58        Response.Headers[_deprecationHeaderName] = "true";
 59        Response.Headers.Link = "</api/v1/application-information>; rel=\"successor-version\"";
 60
 61        // Sunset is advertised only when the application has configured a real removal date. A template cannot
 62        // supply one: a shipped date is arbitrary until it passes, and misleading afterwards.
 63        if (_apiVersioningOptions.DeprecatedVersionSunset is DateTimeOffset sunsetDate)
 64        {
 65            Response.Headers[_sunsetHeaderName] =
 66                sunsetDate.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
 67        }
 68    }
 69}
 70
 71/// <summary>
 72/// Represents sample application API information.
 73/// </summary>
 74/// <param name="ApplicationName">The application name.</param>
 75/// <param name="ApiVersion">The resolved API version.</param>
 76/// <param name="Message">A short status message.</param>
 677public sealed record ApplicationInformationResponse(
 678    string ApplicationName,
 679    string ApiVersion,
 680    string Message);