dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.46k stars 10.03k forks source link

Allow non-attribute routing for ApiController #43192

Open jez9999 opened 2 years ago

jez9999 commented 2 years ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

I am trying to use non-attribute routing for my ApiController but when I try to UseEndpoints() throws the exception:

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

Describe the solution you'd like

ApiControllers should support non-attribute routing.

Additional context

It's regularly stated that attribute routing becomes a requirement when a controller is an ApiController. What I don't understand is why. It seems completely arbitrary. Can't it be opened up to non-attribute routing, or does this cause some technical problem?

captainsafia commented 2 years ago

I am trying to use non-attribute routing for my ApiController

What is your motivation for using non-attribute routing for your controller? Have you considered using something other than ApiController? ApiControllers currently rely on attribute routing by design.

ghost commented 2 years ago

Hi @jez9999. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

jez9999 commented 2 years ago

@captainsafia I can use attribute routing. I'm just interested as to why that design restriction is there when someone might prefer to use convention-based routing.

BrennanConroy commented 2 years ago

Triage: We should investigate whether we can support non-attribute routing for ApiController's now without breaking back-compat.

hzk120 commented 1 year ago

@captainsafia Use case for this request can be : Defining a route prefix at a single place so it is applied to all controllers. Right now, we have to change the attribute on every Controller. If an alternative to that problem exists , please suggest.

Problem Reference Link: https://stackoverflow.com/questions/73387620/how-to-add-api-prefix-to-every-end-point-in-an-asp-net-core-web-api