| | | 1 | | using Asp.Versioning; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Web.Controllers.Api; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides sample versioned API endpoints for the application. |
| | | 8 | | /// </summary> |
| | | 9 | | [ApiController] |
| | | 10 | | [ApiVersion("1.0")] |
| | | 11 | | [ApiVersion("0.9", Deprecated = true)] |
| | | 12 | | [Route("api/v{version:apiVersion}/application-information")] |
| | | 13 | | [Route("api/application-information")] |
| | | 14 | | public sealed class ApplicationInformationController : ControllerBase |
| | | 15 | | { |
| | | 16 | | private const string _deprecationHeaderName = "Deprecation"; |
| | | 17 | | private const string _sunsetHeaderName = "Sunset"; |
| | | 18 | | |
| | 2 | 19 | | private static readonly DateTimeOffset _deprecatedVersionSunsetDate = |
| | 2 | 20 | | new(2026, 12, 31, 23, 59, 59, TimeSpan.Zero); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Returns application API information for the requested API version. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <returns>Application API version information.</returns> |
| | | 26 | | [HttpGet] |
| | | 27 | | public ActionResult<ApplicationInformationResponse> Get() |
| | | 28 | | { |
| | 6 | 29 | | ApiVersion requestedVersion = HttpContext.Features |
| | 6 | 30 | | .Get<IApiVersioningFeature>() |
| | 6 | 31 | | ?.RequestedApiVersion ?? new ApiVersion(1, 0); |
| | | 32 | | |
| | 6 | 33 | | if (requestedVersion.MajorVersion == 0 && requestedVersion.MinorVersion == 9) |
| | | 34 | | { |
| | 2 | 35 | | AppendDeprecationHeaders(); |
| | | 36 | | } |
| | | 37 | | |
| | 6 | 38 | | return Ok(new ApplicationInformationResponse( |
| | 6 | 39 | | ApplicationName: "ProjectTemplate.Web", |
| | 6 | 40 | | ApiVersion: FormatApiVersion(requestedVersion), |
| | 6 | 41 | | Message: "API versioning foundation active.")); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | private static string FormatApiVersion(ApiVersion version) |
| | | 45 | | { |
| | 6 | 46 | | int minorVersion = version.MinorVersion ?? 0; |
| | | 47 | | |
| | 6 | 48 | | return string.Create( |
| | 6 | 49 | | System.Globalization.CultureInfo.InvariantCulture, |
| | 6 | 50 | | $"{version.MajorVersion}.{minorVersion}"); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private void AppendDeprecationHeaders() |
| | | 54 | | { |
| | 2 | 55 | | Response.Headers[_deprecationHeaderName] = "true"; |
| | 2 | 56 | | Response.Headers[_sunsetHeaderName] = _deprecatedVersionSunsetDate.ToString("R", System.Globalization.CultureInf |
| | 2 | 57 | | Response.Headers.Link = "</api/v1/application-information>; rel=\"successor-version\""; |
| | 2 | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Represents sample application API information. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="ApplicationName">The application name.</param> |
| | | 65 | | /// <param name="ApiVersion">The resolved API version.</param> |
| | | 66 | | /// <param name="Message">A short status message.</param> |
| | | 67 | | public sealed record ApplicationInformationResponse( |
| | | 68 | | string ApplicationName, |
| | | 69 | | string ApiVersion, |
| | | 70 | | string Message); |