Reputation: 1813
I am creating a new website and web api. Using the VS2013 template I select the webapi template (with individual login). The template creates a mvc asp site.
Is there anything wrong with having my website in the same project as my web api or should I create two projects in my solution. One for the API and one for the website?
I am thinking of allowing a desktop app to use the api as well.
Upvotes: 4
Views: 2151
Reputation: 9043
As Brian said there is nothing wrong, but it is not only about code organization, if you host them in same project then there is no need to enable CORS for API if you are building SPA (issuing XHR requests from browser), they both will be on the same domain. As well from security perceptive it will get complicated because you will use cookies for the MVC application and bearer tokens for the API. I'm not big fan for the VS 2013 Web API template with individual accounts because it add lots of middle wares and boilerplate code that you might need to use or not aware of its functionality. My recommendation is to separate them from the beginning and have control on both projects. You can read more on how to build API from scratch and components you need here.
Upvotes: 4
Reputation: 19232
No there is nothing "wrong" with including the WebAPI code in the ASP.NET MVC project. At its simplest, having the WebAPI in a separate project is merely code organization, having any code in another project is about code organization. If your project is small, will remain small, and you are the only one working on the project than it really is fine to leave API in the same project as the ASP.NET MVC website.
However, if you expect other people to work on the project, if you expect the code base to grow. Than you should organize your code well from the start and place the WebAPI in a separate project.
But for a shorter answer: it is wrong, you should always be tightly organizing your code at every moment or it will get out of control, be disorganized and hard to maintain in the future.
So put the WebAPI in a separate class library project.
Upvotes: 1