Why do my ASP.NET Core API endpoints respond with 404 errors?

All API requests return a 404 error even though routing and controllers appear correctly configured. None of the actions are being executed. Check this revised controller example:

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SampleAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SalesController : ControllerBase
    {
        private readonly ISalesService _service;
        public SalesController(ISalesService service) { _service = service; }

        [HttpGet]
        public async Task<IActionResult> RetrieveSales()
        {
            var results = await _service.FetchAllAsync();
            return Ok(results);
        }
    }
}

I encountered a similar problem while working on an API. In my case, the issue was due to misconfiguration in the middleware setup in Startup.cs. The order of UseRouting and UseEndpoints was not correctly arranged. Once I ensured that UseRouting was called before UseEndpoints, the API endpoints started functioning as expected. It is also useful to double-check that the attribute routing on controllers is consistent with the expected URL path configuration. These steps resolved the 404 errors I was facing.

try checking if your route is exactly matching the controller name; sometimes a small typo or case sensitivity could be the culprit. also, mabe your requests havent been setup properly in startup.

hey, i had simlar issues, it was a middleware misorder in my program.cs. double-check your launchsettings and ensure app.userouting comes before endpoints in your startup. sometimes a tiny configuration slip causes these 404s, hope this helps!

I experienced similar issues in one of my projects and eventually discovered that the problem was due to a missing middleware component in my Startup configuration. Although the controllers were set up correctly, I had not properly registered all necessary services and middleware, which inadvertently caused the API endpoints to return 404 errors. After reviewing my configuration and ensuring that services were added before routing was applied, I resolved the issue. Verifying that the version of ASP.NET Core and the launch settings did not conflict with the new routing conventions also contributed to the solution.

In my experience, another cause for the 404 errors was a mismatch between the route template and the actual URL call. In one project, I discovered that the routing attribute was slightly different from what the client was requesting, which led to the errors. Verifying that the controller name in the route matches exactly with the URL segment helped resolve the issue. It is also advisable to ensure that any custom route configurations in appsettings or additional middleware do not interfere with the default routing behavior.