ASP.NET Core routing options
Say, I have a single server for some views and some apis, so I'd route views by default, while the api would go under /api. The question is what's the correct way to achieve the routing?
Ideally it would be the second option, but the first one also seems fine, if the constant were to be factored out.
- Am I supposed to use this https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-6.0#areas , stick an
[Area("api")]on all api controllers? - Is there a way to have all api controllers be imported from a separate assembly, making them end up in the right grouping on the bases of assembly? Or by namespace prefix?
- Should I explicitly stick a
Routeto all controllers?
/api/whatever, while all the rest should go to /. I've read the entire page linked above, but it doesn't really mention such a use case, and suggests no best practices in that regard. Ideally it would be the second option, but the first one also seems fine, if the constant were to be factored out.
Learn how ASP.NET Core MVC uses Routing Middleware to match URLs of incoming requests and map them to actions.
