diff --git a/aspnetcore-webapi/ReturnTypes/APIReturnType/APIReturnType.csproj b/aspnetcore-webapi/ReturnTypes/APIReturnType/APIReturnType.csproj
index 60bf9ead83..0268d75ddb 100644
--- a/aspnetcore-webapi/ReturnTypes/APIReturnType/APIReturnType.csproj
+++ b/aspnetcore-webapi/ReturnTypes/APIReturnType/APIReturnType.csproj
@@ -1,13 +1,13 @@
- net6.0
+ net10.0
enable
enable
-
+
diff --git a/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeActionResultController.cs b/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeActionResultController.cs
index 6d3b075464..35797e2c2c 100644
--- a/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeActionResultController.cs
+++ b/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeActionResultController.cs
@@ -16,7 +16,7 @@ public EmployeeActionResultController(IFakeRepository repository)
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
- public ActionResult GetById(int id)
+ public ActionResult GetById(int id)
{
if (!_repository.TryGetEmployee(id, out var employee))
{
@@ -31,15 +31,14 @@ public EmployeeActionResultController(IFakeRepository repository)
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task> CreateAsync(Employee employee)
{
- int? employeeNameLength = employee?.Name?.Length;
- if (employeeNameLength < 3 || employeeNameLength > 30)
+ if (employee.Name is not { Length: >= 3 and <= 30 })
{
return BadRequest("Name should be between 3 and 30 characters.");
}
await _repository.AddEmployeeAsync(employee);
- return CreatedAtAction(nameof(GetById), new { id = employee?.Id }, employee);
+ return CreatedAtAction(nameof(GetById), new { id = employee.Id }, employee);
}
}
}
diff --git a/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeHttpResultsController.cs b/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeHttpResultsController.cs
new file mode 100644
index 0000000000..92f7c3116b
--- /dev/null
+++ b/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeHttpResultsController.cs
@@ -0,0 +1,41 @@
+using Microsoft.AspNetCore.Http.HttpResults;
+using Microsoft.AspNetCore.Mvc;
+
+namespace APIReturnType.Controllers
+{
+ [Route("api/[controller]")]
+ [ApiController]
+ public class EmployeeHttpResultsController : ControllerBase
+ {
+ public IFakeRepository _repository;
+
+ public EmployeeHttpResultsController(IFakeRepository repository)
+ {
+ _repository = repository;
+ }
+
+ [HttpGet("{id}")]
+ public Results> GetById(int id)
+ {
+ if (!_repository.TryGetEmployee(id, out var employee))
+ {
+ return TypedResults.NotFound();
+ }
+
+ return TypedResults.Ok(employee);
+ }
+
+ [HttpPost]
+ public async Task, Created>> CreateAsync(Employee employee)
+ {
+ if (employee.Name is not { Length: >= 3 and <= 30 })
+ {
+ return TypedResults.BadRequest("Name should be between 3 and 30 characters.");
+ }
+
+ await _repository.AddEmployeeAsync(employee);
+
+ return TypedResults.Created($"/api/employeehttpresults/{employee.Id}", employee);
+ }
+ }
+}
diff --git a/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeIActionResultController.cs b/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeIActionResultController.cs
index 067f5fb241..435b3a3f59 100644
--- a/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeIActionResultController.cs
+++ b/aspnetcore-webapi/ReturnTypes/APIReturnType/Controllers/EmployeeIActionResultController.cs
@@ -14,7 +14,7 @@ public EmployeeIActionResultController(IFakeRepository repository)
}
[HttpGet("{id}")]
- [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Employee))]
+ [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetById(int id)
{
@@ -31,7 +31,7 @@ public IActionResult GetById(int id)
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task CreateAsync(Employee employee)
{
- if (employee.Name.Length < 3 || employee.Name.Length > 30)
+ if (employee.Name is not { Length: >= 3 and <= 30 })
{
return BadRequest("Name should be between 3 and 30 characters.");
}
diff --git a/aspnetcore-webapi/ReturnTypes/APIReturnType/FakeRepository.cs b/aspnetcore-webapi/ReturnTypes/APIReturnType/FakeRepository.cs
index 89aa95d847..639c66bf72 100644
--- a/aspnetcore-webapi/ReturnTypes/APIReturnType/FakeRepository.cs
+++ b/aspnetcore-webapi/ReturnTypes/APIReturnType/FakeRepository.cs
@@ -1,4 +1,6 @@
-namespace APIReturnType
+using System.Diagnostics.CodeAnalysis;
+
+namespace APIReturnType
{
public class FakeRepository : IFakeRepository
{
@@ -31,7 +33,7 @@ public IEnumerable GetEmployees()
return Employees;
}
- public bool TryGetEmployee(int id, out Employee? employee)
+ public bool TryGetEmployee(int id, [NotNullWhen(true)] out Employee? employee)
{
employee = GetEmployees().FirstOrDefault(e => e.Id == id);
return employee != null;
diff --git a/aspnetcore-webapi/ReturnTypes/APIReturnType/IFakeRepository.cs b/aspnetcore-webapi/ReturnTypes/APIReturnType/IFakeRepository.cs
index 43110e675b..6a01cad6f7 100644
--- a/aspnetcore-webapi/ReturnTypes/APIReturnType/IFakeRepository.cs
+++ b/aspnetcore-webapi/ReturnTypes/APIReturnType/IFakeRepository.cs
@@ -1,10 +1,12 @@
-namespace APIReturnType
+using System.Diagnostics.CodeAnalysis;
+
+namespace APIReturnType
{
public interface IFakeRepository
{
public IEnumerable GetEmployees();
- public bool TryGetEmployee(int id, out Employee? employee);
+ public bool TryGetEmployee(int id, [NotNullWhen(true)] out Employee? employee);
public IEnumerable GetActiveEmployees();
diff --git a/aspnetcore-webapi/ReturnTypes/APIReturnType/Program.cs b/aspnetcore-webapi/ReturnTypes/APIReturnType/Program.cs
index 5d43572f2b..47d08c73ec 100644
--- a/aspnetcore-webapi/ReturnTypes/APIReturnType/Program.cs
+++ b/aspnetcore-webapi/ReturnTypes/APIReturnType/Program.cs
@@ -6,9 +6,7 @@
// Add services to the container.
builder.Services.AddControllers();
-// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
-builder.Services.AddEndpointsApiExplorer();
-builder.Services.AddSwaggerGen();
+builder.Services.AddOpenApi();
builder.Services.AddSingleton();
@@ -17,8 +15,7 @@
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
- app.UseSwagger();
- app.UseSwaggerUI();
+ app.MapOpenApi();
}
app.UseHttpsRedirection();
diff --git a/aspnetcore-webapi/ReturnTypes/Tests/Tests/Tests.csproj b/aspnetcore-webapi/ReturnTypes/Tests/Tests/Tests.csproj
index 196a9e29bd..a25fabdc58 100644
--- a/aspnetcore-webapi/ReturnTypes/Tests/Tests/Tests.csproj
+++ b/aspnetcore-webapi/ReturnTypes/Tests/Tests/Tests.csproj
@@ -1,21 +1,21 @@
- net6.0
+ net10.0
enable
false
-
-
-
-
+
+
+
+
runtime; build; native; contentfiles; analyzers; buildtransitive
all
-
+
runtime; build; native; contentfiles; analyzers; buildtransitive
all