Using ASP.NET MVC as Web Service

33,951

Solution 1

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

Solution 2

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.

Solution 3

New ASP.NET MVC includes Web Api Kit, which can do exactly what you want. With current version you can still use it. There are no real drawbacks to it

Share:
33,951
Henry Aung
Author by

Henry Aung

Updated on February 05, 2020

Comments

  • Henry Aung
    Henry Aung over 4 years

    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!