1

I am developing an MVC5 application and use Entity Framewerok 6 code first on this. Now we we will also develop an android application that will interact with the MVC application (CRUD operations) by using the web services. At this stage I want to be clarified about the issues below:

1) I think WebAPI is better option for us as we use the services on android apps. What do you suggest?

2) In order to integrate WebAPI to an MVC project, which changes should be made? On the other hand, can we use the same controller and data layer methods (i.e. SaveChanges, etc.) by making some modifications i.e. inheritance? Or do we have to create a seperate methods for web services? Could you give an example by code?

3) Does integrating WebAPI to the MVC project affect the MVC project's abilities or methods? I mean that is there any disadvantage integrating WebAPI to an MVC project?

Any help would be appreciated.

Jack
  • 1
  • 21
  • 118
  • 236

1 Answers1

1

1) That's a good idea. Web API is easy to implement and consume

2) You don't need to make changes to intergate Web API in your application: just start using it. As you want to expose CRUD operations from EF a good idea would be to implement ODATA services. Or use something like Breeze (depending on how you want to consume the services). See "MVC and Web API" bwelow

3) Web API doesn't affect at all the MVC part, unless you make a mistake setting the routes. Although they run in the same host, they work completely independent of each other.

MVC and Web API

Unless you need to do something special, like exposing Web API in a different URL or "domain name", MVC and Web API are implemented in the same web application project. To start using Web API in your MVC project simply add a new controller. Perhaps you'll have to include also the WEB API route configuration, and some other Web API configuration.

If you want to expose the EF model throug Web API you simply have to follow the instructions in the link to create an ODATA controller, which will expose the EF model as a RESTful service, allowing you to execute the CRUD operations to the EF model through URLs.

NOTE: What you want to do is a very frequesnt pattern in MVC applications: MVC is used for generating the views, and Web API fos exposing functionalities that can be easily consumed from the views usin Javascript + AJAX. Don't be afraid to use it. You'll find no problems at all

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • Thanks for reply. From you answers, I understand that for example selecting, creating or updating data, there is no need to create extra controllers or methods for web services (assuming that the web service will also use the same methods with the same parameters). On the other hand, do I have to create an extra WebAPI project? If not, what changes will I do on Controllers, CRUD methods on data layer, route config, etc? Could you give an example code by updating your answer please? – Jack May 18 '15 at 08:08
  • I don't understand very well your first question. I've updated my answer. Basically you need to create new ODATA controllers to expose the EF. You cannot use the same MVC controllers, because they do something different, like returning views. But implementing ODATA is super fast. I'ma afraid I cannot do an example of such a big project, but I can solve your concrete doubts. – JotaBe May 18 '15 at 08:17
  • Sorry, I am new to WebAPI and web services and I need a little bit more help. So, do you meant that I need to use OData in order to create WebAPI service in my application? On the other hand, in the WebAPI tutorials, the WebAPI checkbox is selected while creating MVC project. By selecting this what is changed in the MVC project? For the last stage, I need the code sampe on your answer. Any help please? Thanks in advance. – Jack May 18 '15 at 08:19
  • As to the ODATA part, please read the info in the link. Please understand that making a whole solution with EF model, and exposing it through ODATA and showing fragments of the resulting code is completely helpless. It's not that I cannot do it, it'sthat it makes no sense. Seeing the final code wouldn't help you. Please, follow the tutorials. As to the Web API part, please, do also look tutorials here to understand the basics: http://www.asp.net/web-api. They explain clearly the confiurations, teh routing, and so on.if you have some doubt after seeing it, don't hesitate to ask for help – JotaBe May 18 '15 at 08:26
  • Of course it is impossible giving an example including the entire code and I just want you give a code sample for the controllers actually. On the other hand I read the article carefully again and some doubts below >>> – Jack May 18 '15 at 09:01
  • 1) Do you know what is the difference between selecting only WebAPI and only MVC at the stage of "Create the Visual Studio Project" on the tutorial? I see the following differences: WebApiConfig.cs, some extra references, Controller inherits from ApiController, WebApiConfig.Register in the global.asax. IS there any? Making these changes to an MVC project would be enough to use webAPI? – Jack May 18 '15 at 09:01
  • 2) I think ODATA has some advantages. But do I have to use it in order to use WebAPI? Or is it also possible to use WebAPI by making the changes I indicated on the previous question (1) i.e. and the inheriting from ApiController etc. above? – Jack May 18 '15 at 09:03
  • 1) Please, use VS option "Add new item" "Web/Wep API/Web API Controller v2". It will create a controller. And, most probably, it will work from the beginning (it depends on how you started your project) 2) ODATA works on Web API, i.e. an ODATA controller is a Web API controller that allows to easily expose an EF model. *) Again, I suggest you taking the first tutorials of ODATA and We API to clearly understand what configuration they need, how they are invoked an so on. In fact in 1) you're practically answering yourself... if not sure, try and ask if you get some errors os something fails – JotaBe May 18 '15 at 09:11
  • Ok, the final question is that: In teh WebAPI, the service requests (made from android app for example) are met in the controller stage as the web application. So, the requests are something like that: localhost/ApiController/GetData/3 By doing this, the requests are prevent from accessing directly to the data layer in the project (assuming I use three layer project). – Jack May 18 '15 at 09:24
  • If you really need to do so, don't use OData and implement the necessary WebAPI controllers using your data layer. It will take more time, but if that's a requirement, that's the way to go. ODATA is a "quick & dirty" solution. – JotaBe May 18 '15 at 09:38
  • Many thanks for your answers (**voted+**). Finally I decide to integrate WebAPI 2 with my MVC5 project as on [Integrating ASP.NET Web Forms, MVC and Web API](http://www.asp.net/aspnet/overview/whats-new-in-visual-studio-2013/one-aspnet-integrating-aspnet-web-forms,-mvc-and-web-api). On the other hand, keeping in mind **security** and **maintennace concerns**, would you create a seperate project for WebAPI or would you use it on the same MVC project's UI layer (presentation layer)? – Jack May 19 '15 at 08:53
  • Please, open a new question for this so that other people can easily find it (or answer it, with good ideas). All which we say in the comments is virtually inexistent. It's good to make comments to clarify aspects of the same question, but not to making new questions. – JotaBe May 19 '15 at 11:23
  • Sorry, I create the new question on [here](http://stackoverflow.com/questions/30330403/is-it-better-to-create-a-separate-project-for-webapi). Could you have a look at please? Thanks... – Jack May 19 '15 at 15:53