g.pickardou
g.pickardou

Reputation: 35963

What is the advantage of using Web API in a ASP.NET MVC (5) project?

I am implementing an enterprise web application in ASP.NET MVC 5. In many situations I am writing AJAX gets and posts and communicate with the server app. Currently I am writing controller actions to serve those requests, typically returning with JSON result. The action methods parameter binding also seems work seamlessly when I passing JSON at client side.

I do not want to go far with a not appropriate practice, so the question arises, what could be the advantage to add Web API support to my project, and refactor my current ajax - controller practice?

Upvotes: 4

Views: 8249

Answers (4)

Minhajuddin Khaja
Minhajuddin Khaja

Reputation: 121

  1. It’s simple, scalable and robust because it supports all the MVC features such as routing, controllers, model binders, IOC container, action results, filter, or dependency injection as it is built on top of Asp.Net MVC.

  2. Empower the developers by handing over control over the way HTTP protocol messages are sent and responded to.

  3. Unit testing is easy as it support test driven development.

  4. It provides ample flexibility in web API creation due to content negotiation and drives support for the ASP.NET routing.

  5. Unlike WCF REST services, there is no need to define tedious configuration settings for different devices.

  6. The architecture of web APIs is very light, which makes them a perfect alternative for the developers when they want to build applications for devices with limited bandwidth.

  7. Web APIs are used to create non-SOAP-based HTTP Services, so if there is a requirement for web services, but not SOAP, then ASP.Net Web API is great to look for.

  8. Supports different message format like json, plain text and xml.

  9. Asp.Net Web API supports convention-based CRUD Actions since it works with HTTP verbs GET, POST, PUT and DELETE.

Upvotes: 0

Darani
Darani

Reputation: 11

  1. Light weight & Easy to use.
  2. Less data transfer between client and server.
  3. Using webapi we can develop cross domain application.
  4. Less configuration needed as compared to WCF.

Upvotes: 1

kamil-mrzyglod
kamil-mrzyglod

Reputation: 5008

There are few advantages according using WebAPI over MVC + Ajax:

  1. Internal serialization

    WebAPI has an internal serialization, which makes returning specific data much more easier, without your own extension method or making your controller dependent on the serialization framework library.

  2. Action result helpers

    You can use plenty of action result helpers like Ok(), NotFound(), InternalServerError(), which all return IHttpActionResult, what makes your code easier to read and maintain and clearly state your intention.

  3. Action result abstraction

    Using IHttpActionResult you can easy abstract result, which is helpful when unit testing your controllers.

  4. Available to self-host

    Using OWIN you can easily self-host your WebAPI project, what makes your application much easier to maintain and IIS-independent.

  5. Native attribute routing

    WebAPI has implemented attribute routing which makes all routing configuration much more easier(and helps when using feature-based architecture).

  6. DI configuration improvements

    Both WebAPI and MVC have their own composition roots and different DI implementation. Microsoft has introduced some improvements in WebAPI to make it easier to use and maintain.

I'd say that using MVC + Ajax was viable only when WebAPI didn't exist because there was the only option.

Upvotes: 4

Mustafa ASAN
Mustafa ASAN

Reputation: 3835

If your project clients needs data in multiple formats (json,xml,csv) or have chance to change in future Wep Api needs minimal configuration comparing to mvc. Wep Api returns data to client according to content negotiation (if client needs xml returns xml,if json return json according to request header ) but in mvc you need more code to satisfy that.You have to explicitly specify data format when writing action methods.(JsonResult,ActionResult,XmlResult)

Wep Api gives you more meaningful idea about what you are doing when you look at the code later.Comparing method signatures; public List<Student> Get() has more meaning than public JsonResult Index().

Upvotes: 1

Related Questions