C
C#16mo ago
Pibbles

❔ ASP .NET Core Controller Default Route Issue

I have a controller (TestController) with an Index action method. The url I'm using is "localhost:8372/Test" This returns a 404 error If I add [Route("[controller]")] the controller works as expected. I just created the application but I thought that [Route("[controller]")] was the default route. Is it not?
9 Replies
Kouhai
Kouhai16mo ago
Route("[controller]") Means the route would be bound to this class' name (the class annotated with the attribute) Notice controller is between []
Pibbles
PibblesOP16mo ago
yeah I see that but isn't that how it normally is? I swear you could create a controller called "RaccoonController" and the path would default to /controller/action or in this case /raccoon/action
Kouhai
Kouhai16mo ago
You want this? Route("[controller]/[action]")
Pibbles
PibblesOP16mo ago
that's what this does I want to know what the default is, because I swear what I added is just the same as the default
Kouhai
Kouhai16mo ago
I'm confused right now, sorry wdym by default 😅 ? default for what?
Pibbles
PibblesOP16mo ago
you don't have to specify a path on the controller
Kouhai
Kouhai16mo ago
Oh! If your controller has ApiController attribute then you must add Route attribute afaik, but if the controller doesn't have ApiController you can do something like app.MapControllerRoute("default", "{controller=Home}/{action=Get}");
Pibbles
PibblesOP16mo ago
okay well good news this does work, I'm still confused as to what the default mapping is, and if they just removed it? I feel like there's no way they completely removed it but for now this is a nice implementation so thanks
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server