I have an MVC application that uses MVC Controllers to return views. Now I want to expose an API to other applications to consume, as well as returning JSON data types for some SPA features in the same MVC. What are the differences between adding Web API Controllers to my MVC Project vs adding a whole new Web API Project?
-
This question is too vague because the topic is big. Are there specific differences (or areas) you are concerned about? If not, I'd just add them to your current project because you can always migrate them to a separate project later.Jeff S– Jeff S2016-05-03 16:34:13 +00:00Commented May 3, 2016 at 16:34
-
I understand. As far as I know, I just need to add Web Api Controllers to my current project, but I wanted to know what would be the benefits of adding a separate Web Api Project and create them there.Hernan Demczuk– Hernan Demczuk2016-05-03 16:37:48 +00:00Commented May 3, 2016 at 16:37
2 Answers
returning JSON data types for some SPA features in the same MVC
For that case, I'll place Web API inside existing MVC. By doing so, you can share business logic, services and even models.
In my case, I have SPA silos using AngularJS, and both MVC and Web API live happily in the same web application, and share business logic and data access layers.
It is worth noting that you can share Authentication cookie if you keep MVC and Web API together. Otherwise, it is pain in the neck to authenticate in both places at the same time, because Web API is token based and MVC is cookie based by default.
FYI: In new ASP.Net MVC 5, there won't be separate MVC and Web API anymore.
1 Comment
The only difference between the two is reusability and good design practice. I highly recommend to use it in a separate project. Then, later on, you will be able to reuse it without much or any effort.
Another advantage to segregate is the impact on testing required for any changes made. If you keep the projects different then if later on you change anything, the domain for the re-testing efforts would also be just the second project.