'Using ApiControllerAttribute without using RouteAttribute

In ASP.NET Core (v 2.1.5) you can create controllers without having them inherit from Controller class (as you know). And if you do, you have to use RouteAttribute to define your routes. But, I'm interested if we can use implicit routing (and not attribute routing) along with ApiController attribute together. Example: Having this implicit routing in Startup.cs:

app.UseMvc(routeBuilder => 
{
    routeBuilder.MapRoute("api_default", "{controller}/{action}/{id?}");
});

And this Cotroller

[ApiController]
public class ValuesController 
{
    [HttpGet] 
    public string Get(int id) => id.ToString();
}

Will throw this exception:

InvalidOperationException: Action 'TestApi.Controllers.ValuesController.Get' does not have an attribute route. Action methods on controllers annotated with ApiControllerAttribute must be attribute routed.

Is there a way to avoid the exception?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source