Skip to content

Web API return types: retarget net10.0, built-in OpenAPI, HttpResults controller - #2213

Merged
vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/73621-aspnetcore-web-api-return-types
Sep 19, 2026
Merged

vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/73621-aspnetcore-web-api-return-types

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Retargets aspnetcore-webapi/ReturnTypes from net6.0 to net10.0 and modernises it, for the republish of "IActionResult and Web API Return Types in ASP.NET Core".

  • Swashbuckle.AspNetCore 6.2.3 removed. Microsoft.AspNetCore.OpenApi 10.0.12 added, with AddOpenApi() and MapOpenApi() replacing AddEndpointsApiExplorer()/AddSwaggerGen() and UseSwagger()/UseSwaggerUI(). 10.0.12 rather than the template's 10.0.10, which still restores with NU1903 on Microsoft.OpenApi 2.0.0.
  • Test packages: Microsoft.AspNetCore.Mvc.Testing 10.0.12, Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1.
  • EmployeeIActionResultController.CreateAsync raised CS8602 after the retarget (employee.Name.Length dereferences a string?). The guard is now employee.Name is not { Length: >= 3 and <= 30 }, and the same expression replaces the int? workaround in EmployeeActionResultController.
  • EmployeeActionResultController.GetById returns ActionResult<Employee> instead of ActionResult<Employee?>, and TryGetEmployee gains [NotNullWhen(true)] on the interface and the implementation so the implicit conversion stays warning free.
  • The 200 attribute uses the generic [ProducesResponseType<Employee>] form.
  • New EmployeeHttpResultsController covers the fourth return type, Results<T1, T2> through TypedResults, which the article did not have a sample for.

Build on SDK 10.0.302: 0 warnings, 0 errors. dotnet test: 7 passed, 0 failed, 0 skipped.

… controller

Retargets aspnetcore-webapi/ReturnTypes from net6.0 to net10.0 and modernises it.

- Swashbuckle.AspNetCore 6.2.3 removed; Microsoft.AspNetCore.OpenApi 10.0.12 added,
  with AddOpenApi() and MapOpenApi() replacing AddEndpointsApiExplorer/AddSwaggerGen
  and UseSwagger/UseSwaggerUI.
- Test packages: Mvc.Testing 10.0.12, Test.Sdk 18.10.1, xunit 2.9.3,
  xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1.
- Fixes CS8602 in EmployeeIActionResultController.CreateAsync by replacing the
  employee.Name.Length guard with a pattern, and applies the same pattern in
  EmployeeActionResultController in place of the int? workaround.
- EmployeeActionResultController.GetById returns ActionResult<Employee> instead of
  ActionResult<Employee?>, and TryGetEmployee gains [NotNullWhen(true)] on the
  interface and the implementation so the implicit conversion stays warning free.
- The 200 attribute uses the generic [ProducesResponseType<Employee>] form.
- New EmployeeHttpResultsController shows the fourth return type, Results<T1, T2>
  through TypedResults.

Build: 0 warnings, 0 errors. Tests: 7 passed, 0 failed, 0 skipped.
@vladimir-pecanac-main
vladimir-pecanac-main merged commit 9e2637b into CodeMazeBlog:main Sep 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant