Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ASP.NET MVC as Web Service

Does anyone have experience using ASP.NET MVC project as a Web Service?

i.e. using ASP.NET MVC without Views, so other applications can use the URL to GET or POST to the actions in the Controller.

Has anyone used it? If so, are there any drawbacks for not using Web Service project instead?

Thank you all in advance!

like image 270
Henry Aung Avatar asked May 28 '12 01:05

Henry Aung


People also ask

What is web service in ASP.NET MVC?

ASP.NET MVC 5 for Beginners A web service is a web-based functionality accessed using the protocols of the web to be used by the web applications. There are three aspects of web service development: Creating the web service. Creating a proxy.

Can I use MVC controller as Web API?

In order to add a Web API Controller you will need to Right Click the Controllers folder in the Solution Explorer and click on Add and then Controller. Now from the Add Scaffold window, choose the Web API 2 Controller – Empty option as shown below. Then give it a suitable name and click OK.

Why Web API is used in MVC?

ASP.NET Web API can be used with MVC for any type of application. A web API can help you develop ASP.NET application via AJAX. Hence, web API makes it easier for the developers to build an ASP.NET application that is compatible with any browser and almost any device.


2 Answers

It really depends on the kind of application you're writing. I would actually argue the inverse of LukLed's position - SOAP-based services are better suited for internal clients when you want to support things like Windows Authentication, or different protocols like TCP or MSMQ.

Using a more web-style of GETs and POSTs around specific "resources" starts to get you into the REST architectural style. This technique has a few distinct advantages to me:

  • The response is usually smaller, particularly when using lightweight formats like JSON
  • Because of the simplicity of the requests and responses, this makes it much easier to use in mobile / native applications (see Twitter's API, for example)
  • The service you create can be self-describing and discoverable, because you can link to other parts of your API just like normal web pages.

One article that particularly helped me understand the tradeoffs here is Martin Fowler's "Steps Toward the Glory of REST." That being said, it may or may not be the right fit for your application.

If you do choose to build a more REST-based service, definitely consider using the ASP.NET Web API built into MVC4 as others have mentioned. It's currently in beta, but Microsoft felt good enough about it to give it a go-live license.

UPDATE:

Since ASP.NET core, ASP.NET web API has been integrated into MVC 6 project. https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

like image 69
Brandon Linton Avatar answered Sep 21 '22 20:09

Brandon Linton


If you want to use simple GET and POST calls, MVC will be good choice. ASP.NET MVC 4 will have support for creating HTTP based APIs. You can read about it here: http://www.asp.net/web-api

Web Service created in Web Service project can be easier to consume, because it can generate WSDL file, that can be easily read and used in many different languages (by using SOAP protocol). On the other side, WS can create huge XML responses, that could be many times smaller, if you used your own format.

If you want to spread your web service around the world, allowing SOAP will make life easier for many developers. SOAP can be used by people, who almost have no idea about programming. If you'll use it internally, prefer speed and simple requests and responses, you can use MVC.

like image 33
LukLed Avatar answered Sep 22 '22 20:09

LukLed