asp.net-core-webapiasp.net-mvc-routingasp.net-core-3.1asp.net-core-routing

Cannot hit AspNetCore 3.1 MVC API endpoints when published


AspNetCore 3.1 MVC API. Conventional startup

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

I can hit all endpoints when running on localhost. When I publish, all endpoints return 404.

To test I am publishing to the correct location, I added an endpoint that displays in the browser

public IActionResult Index()
{
    return Content("Users Index");
}

When I browse to https://subweb.mainweb/users/api/users/indexI see Users Index returned in the browser, but all other endpoints return 404.

The controller is decorated

[Route("api/Users/[action]")]
public class UsersController : ControllerBase

All other endpoints are decorated eg

[HttpGet]
[ActionName(nameof(IsRegisteredUser))]
public IActionResult IsRegisteredUser()

But I don't see how this would make a difference between running locally and on the server.

How could it be that all endpoints are available in localhost, but only the test endpoint when published? Where do I begin to look?


Solution

  • When I browse to https://subweb.mainweb/users/api/users/indexI see Users Index returned in the browser, but all other endpoints return 404.

    Based on your above example URL, it seems that you host your ASP.NET Core Web API app as an sub-application (sub-app) at /users on your server.

    To access the endpoint(s), you should make sure you include the sub-app's pathbase in URL, otherwise it would result in a 404 - Not Found error.