C#C
C#2y ago
S-IERRA

ASP.Net IActionResult always returns Ok

I have the following method inside of a service, I call this method from a controller

Where its suppsoe to return 404 not found it enters the return statement and jumps to the last line of the method (Return Ok)

This seems to happen on all methods, possibly there is a better way of returning values from services?
    public async Task<IActionResult> DeleteServer(int id)
    {
        await using var dbContext = await _dbContextFactory.CreateDbContextAsync();

        if (await dbContext.Servers.FirstOrDefaultAsync(x => x.Id == id) is not { } server)
            return new NotFoundObjectResult(new ErrorResponse(RestErrors.FailedToFind($"server{id}")));

        dbContext.Servers.Remove(server);
        await dbContext.SaveChangesAsync();

        return new OkResult();
    }


This is how I call this method

    [HttpDelete("{id:int}/delete")]
    public async Task<IActionResult> DeleteServer(int id)
    {
        return await _serverService.DeleteServer(id);
    }
Was this page helpful?