© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•17mo ago•
54 replies
packetphysicist

Returning Service result to Controller (Minimal API)

/// <summary>
/// Deletes a reservation by its ID.
/// </summary>
/// <param name="reservationId">ID of the reservation to delete.</param>
public async Task Delete(int reservationId)
{
    try 
    {
        var reservation = await _context.Reservations
        .FirstOrDefaultAsync(r => r.ReservationId == reservationId);

        if (reservation is not null)
        {
            _context.Reservations.Remove(reservation);
            await _context.SaveChangesAsync();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}
/// <summary>
/// Deletes a reservation by its ID.
/// </summary>
/// <param name="reservationId">ID of the reservation to delete.</param>
public async Task Delete(int reservationId)
{
    try 
    {
        var reservation = await _context.Reservations
        .FirstOrDefaultAsync(r => r.ReservationId == reservationId);

        if (reservation is not null)
        {
            _context.Reservations.Remove(reservation);
            await _context.SaveChangesAsync();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}


I have a method in my service. If my deletion fails, how do I let the controller know it failed?

Endpoint for reference:
/// <summary>
/// Deletes a reservation by ID.
/// </summary>
/// <param name="reservationId">ID of the reservation to delete.</param>
/// <returns>
/// 200 OK status code to confirm that the reservation has been deleted.
/// </returns>
[HttpDelete]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteReservation([FromQuery] int reservationId)
{
    var existingReservation = await reservationStorage.Read(reservationId);

    if (existingReservation == null)
    {
        return NotFound();
    }

    await reservationStorage.Delete(reservationId);
    return Ok();
}
/// <summary>
/// Deletes a reservation by ID.
/// </summary>
/// <param name="reservationId">ID of the reservation to delete.</param>
/// <returns>
/// 200 OK status code to confirm that the reservation has been deleted.
/// </returns>
[HttpDelete]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteReservation([FromQuery] int reservationId)
{
    var existingReservation = await reservationStorage.Read(reservationId);

    if (existingReservation == null)
    {
        return NotFound();
    }

    await reservationStorage.Delete(reservationId);
    return Ok();
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Minimal Api Attributes
C#CC# / help
4y ago
✅ API result
C#CC# / help
3y ago
minimal api DI magic
C#CC# / help
2y ago
✅ Web things minimal api
C#CC# / help
3y ago