C
C#13mo ago
ayman23

❔ API controller implementation inside mvc 5 app

Hello developers, I need help with my project, i am relatively new to .net development but i understand the basics. I already have the existing web app in mvc5 working with a couple of controllers that save some data in a db and generate an excel file. My project is to develop a mobile app with the same ui as the web app (which i have already done using xamarin forms) and send the data from the mobile app to the web app that will be processed using controller logic and saved to the db. The mobile app has is only for entring data and saving model data to db. The backend should be done inside the web app since we already have the logic there once the data is sent via Api I am trying to create an api controller inside my web app that calls the actions in the web app controllers upon receiving model data via the api post request. Is this the right way to do it or there is a better way? ( I had some suggestions creating a new Web Api project and copy all the code from my web app controllers but wouldn't that be redundant?)
3 Replies
BugzyGeek
BugzyGeek13mo ago
My suggestion will be to move the Login from the controller another class which will return the result. This way the MVC Controller and the API Controller can both call the method of the class to get the work done and return the appropriate response. Although you can call the MVC controller Methods just like any other method in a class, MVC controllers normally returns ActionResult so moving the logic into a class that both controllers share will be the appropriate way.
Pobiega
Pobiega13mo ago
You can easily add an Api controller to an MVC app. Just make sure it has the [ApiController] attribute and inherits from ControllerBase
Accord
Accord13mo 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.