C
C#2mo ago
hAssAnNaWaZi

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
5 Replies
glhays
glhays2mo 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
Anton2mo ago
yes, you can I think you can register them manually
Anton
Anton2mo 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
hAssAnNaWaZi
hAssAnNaWaZiOP2mo ago
@glhays @Anton Thank you for responding, i did a lot of debugging and tried many solutions, but I am not able to decide what's going on. Theres something wrong the tag helpers, since I was able to achieve my thing with this @* <form method="post" asp-controller="Auth" asp-action="Login"> *@ <form method="post" action="Auth/Login"> but using the tag helpers it would render the page and it is going to have html like following <form method="post" action="Api/Auth/Login"> however it had to be <form method="post" action="Auth/Login"> which was produced when I used hardcoded action, instead of using taghelper and utilising asp-action thing I am not able to figure out yet that why is the tag helper making such behavior

Did you find this page helpful?