Conventional Routing in ASP.NET Core API

To have conventional routing for your controllers and action, you need to remove [ApiController] attribute and [Route] attribute from your controller and actions and setup route in UseEndpoints.

It's already mentioned in the documentations:

The [ApiController] attribute makes attribute routing a requirement.

Actions are inaccessible via conventional routes defined by UseEndpoints, UseMvc, or UseMvcWithDefaultRoute in Startup.Configure.

Example

This is the working setup that I have for Startup:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

And a sample API controller:

public class ValuesController : ControllerBase
{
    // values/getall
    [HttpGet]
    public IEnumerable<string> GetAll()
    {
        return new string[] { "value1", "value2" };
    }

    // values/getitem/1
    [HttpGet]
    public string GetItem(int id)
    {
        return "value";
    }
}

Have you tried this?

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


EDIT:

I tried to set it up on my machine. When I removed the Route attribute from controller I got below error:

InvalidOperationException: Action 'WebAPISample.Controllers.WeatherForecastController.Index (WebAPISample)' does not have an attribute route. Action methods on controllers annotated with ApiControllerAttribute must be attribute routed.

The error message itself is saying that the API controllers must use attribute routing.

I know this does not answer your question, but with .NET Core 3 APIs, this does not seem to be possible.

From Documentation:

The [ApiController] attribute makes attribute routing a requirement. For example:

[Route("api/[controller]")] 
[ApiController] 
public class ValuesController : ControllerBase

Actions are inaccessible via conventional routes defined by UseMvc or UseMvcWithDefaultRoute in Startup.Configure.

Refer this page on MSDN.