Trouble Setting up double controllers in a single MVC .net project, API controllers and MVC controll

Hi all, Hope so you are having a good one. So basically its a complete web solution which also have a mobile application in the flutter, now for that reason I need to have API controllers. I am bound to use one project, to avoid double hostings and other complexitites that might come along the way. What I did is Created MVC .net project set up API controllers in it about auth and stuff created views and setup MVC controllers as well. Now what's being done is that I have two controllers of the same name , AuthController and AuthMVCController as same present it, but I don't want to use the MVC or API name specifically in the controllers. Is there anyway to have same controller names, like AuthController in APIFolder and AuthController in simple Controllers folder, and distinguish them by [ApiController] and make them work. What I am able to achieve right now is that it do renders the views of the login and stuff, but when I hit the post method in the login page using JS, it just hits to the API controller, however i want it to hit the action of the MVc controller, it would then communciate using httpserive makign request to apicontroller and work accordingly. this behavior ahieves if I have change name of controller like for now its AuthController and AuthMVCController
4 Replies
glhays
glhays2d ago
Cay you do something along these lines.
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

endpoints.MapControllerRoute(
name: "admin",
pattern: "Admin/{controller=Home}/{action=Index}/{id?}",
defaults: new { area = "Admin" },
constraints: new { namespace = "MyApp.Controllers.Admin" });

endpoints.MapControllerRoute(
name: "user",
pattern: "User/{controller=Home}/{action=Index}/{id?}",
defaults: new { area = "User" },
constraints: new { namespace = "MyApp.Controllers.User" });
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

endpoints.MapControllerRoute(
name: "admin",
pattern: "Admin/{controller=Home}/{action=Index}/{id?}",
defaults: new { area = "Admin" },
constraints: new { namespace = "MyApp.Controllers.Admin" });

endpoints.MapControllerRoute(
name: "user",
pattern: "User/{controller=Home}/{action=Index}/{id?}",
defaults: new { area = "User" },
constraints: new { namespace = "MyApp.Controllers.User" });
});
Anton
Anton2d ago
yes, you can I think you can register them manually
Anton
Anton2d ago
you can configure controller providers of this builder start from there also read about controller conventions / application conventions you're going to have to read some docs and look through some of the sources probably to figure out how to do this
Want results from more Discord servers?
Add your server