✅ Controller routing in 2023
Hi, as I understand it, just like everything in programming, you can do one thing in many different ways. This also works to controller routing. There are lot of ways to route your controllers in a web app. What is the most common way to route them in 2023? Setting each method from Program.cs? route attribute? maybe just with method names?
21 Replies
Hi, afaik you create a controllers folder and create controller in their either by keeping controller attribute over the class name or the class name ending with Controller and in program.cs you just add builder.services.AddControllers() and app.MapControllers.
i thing we talk about different things
let me explain it again, what i meant
so, i can
Aahh! Apologies
I usually use route attributes
So, here if i go to "/" it will load home/index
[Route("/")]
also i can set up it like
or like this
Yup, right!
atleast 3 ways and i belive there is more ways to do that
I feel route attributes are cleaner, since you wouldn't clutter program.cs
and im asking, which is most used in our time
so, in case im using attributes, i don't rly need to write anyting in program.cs? like
i have
and thats ok?
but program.cs is like
and controllers in this case is smthing like wrapper for different routes groupd by similar purpose
You can just do app.MapControllers(); instead of mapcontrollerroute
Try adding just app.mapcontrollers(); and run the app
Yes, sir!
That looks very nice and very comfortable way to deal with routing
Isn't it? Builder.service.AddControllers() and app.mapcontrollers() does everything for you
I've learnt this recently too, currently learning more. Answering you helped me too, haha.
Thank a lot for help