Difference Between MVC 5 Project and Web API Project

Difference between MVC 5 Project and Web Api Project

Basically, a Web API controller is an MVC controller, which uses HttpMessageResponse as the base type of its response, instead of ActionResponse. They are the same in most other respects. The main difference between the project types is that the MVC Application project type adds web specific things like default CSS, JavaScript files and other resources needed for a web site, which are not needed for an API.

MVC is used for creating web sites. In this case Controllers usually return a View (i.e. HTML response) to browser requests. Web APIs on the other hand are usually made to be consumed by other applications. If you want to allow other applications to access your data / functionality, you can create a Web API to facilitate this access. For example, Facebook has an API in order to allow App developers to access information about users using the App. Web APIs don't have to be for public consumption. You can also create an API to support your own applications. For example, we created a Web API to support the AJAX functionality of our MVC web site.

Microsoft changed the way they present the different templates. Now instead of using different templates for different project types, they encourage developers to mix ASP.NET technologies inside the same project as needed. Microsoft calls this vNext.

UPDATE: For ASP.NET Core, Web API has been integrated into the MVC 6 project type and the ApiController class is consolidated into the Controller class. Further details at: https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

ASP.NET WebApi vs MVC?

WebApi allows to create services that can be exposed over HTTP rather than through a formal service such as WCF or SOAP.
Another difference is in the way how WebApi uses Http protocol and makes it truly First class Http citizen.

UPDATE: The ASP.NET Core, Web API has been integrated into MVC project type. The ApiController class is consolidated into the Controller class. More at: https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

A relevant link of comparison, discussions & tutorials:

  • MVC5 vs WebApi Project
  • Difference between ASP.NET MVC and
    ASP.NET Web API
  • Introduction to ASP.NET Core includes MVC, Web API demos
  • Getting Started with ASP.NET Web API tutorials

Sample Image

What is the difference between MVC Controller and Web API Controller in ASP.NET MVC 6?

I think you're thinking into this too much.

Your first question "What is the difference of MVC Controller and Web API Controller in ASP.NET MVC 6?" presupposes that they are different, but they are not. They are merged, so there is no difference.

If you want to define separate routes to cordon off your action methods that don't return View results, then go for it. It's up to you how to organize your application. Asking "Which way is now the preferred one to create web apps?" is pointless, since that's up to you to decide for your application, and there's not going to be a more common way of doing things until after MVC 6 has been in production use for a good length of time.

How & where to use Web API2 along with MVC 5 application

That’s a tricky question, you know? But I’ll try to give my opinion, driven by my experience on the development of different applications and services using those two frameworks.

I believe that the most significant difference between those two frameworks lies behind their design goals: MVC was conceived (and has traditionally been used) for building web sites while Web API was born as a framework for building services.

MVC

MVC excels in building web sites and web applications that use a traditional interaction model, based on web browsers as clients, synchronous interactions (with no, or little AJAX) and HTML as primary presentation language (that’s why we usually return Views inside controllers).

From an architectural point of view, MVC could be seen as a framework that spans across Presentation and Service layers: it can build a service that is already a web application through an interface strictly driven by populated Views. While this application type could be defined interoperable by a higher point of view, it surely remains bounded to a single kind of client (the Web Browser).

Web API

Web API, instead, is explicitly designed to be a framework for building services, with many concepts inherited from what we learned implementing Web Services and SOA in the past. Its purpose is to allow a wide range of different devices and clients to consume a service over HTTP. It does not restrict access to only web browsers, but can easily be used as an abstraction layer (the Service Layer mentioned before) between any client application (mobile app, SPA, even other independent services) and the backend/business logic, without losing all the architectural and interoperability benefits given by REST principles and the HTTP protocol.

Web API excels when combined with client-side frameworks like AngularJS, React, Backbone, because it becomes the engine behind data and business logic, allowing those frameworks to create a more fluid and asynchronous interaction between the user and the whole application.

While may be arguable that MVC could be used for such purpose, is undeniable that it was not designed to be a framework for building services consumed by different kinds of clients.

Aren’t they the same?

If we look at the internal architecture of those two frameworks we may find many things and choices in common. This is the main reason why many people do not find a clear distinction between them. In the near future (they are in beta right now) they will be merged into a single framework (MVC6) in ASP.NET Core.

When use them together

In my opinion, there are not many use cases that will require you to use both MVC and Web API in a single project. The reason behind this is related to the different approaches taken in UI rendering: in a MVC Web Application pages are rendered server-side, using a strongly typed ViewModel, while delegating small parts of the rendering to AJAX calls (but those should not be the majority, or else you will find you are asking too much to the framework).

In a modern Web Application that leverage the use of REST services (such as the ones created using Web API) the UI is rendered (almost) entirely client-side, so the need of server-side rendering/model binding is next to nothing: you will compose your UI combining server data (e.g. in JSON format) with static assets (HTML, CSS3, JavaScript) on the client.

Combining the two frameworks could be useful when you need to provide a Web Application in a traditional manner (using MVC), but you also require to offer your services to external clients: your users will use your MVC app, while other services will use the Web API endpoint (similar to what Facebook, Twitter, etc. do).

Another example could be an hybrid approach, where you render part of your web site server-side (using Razor) while the most dynamical section of your site are served using AJAX calls to Web API endpoints (e.g. auto-completes fields, asynchronous calls). But, frankly, I think this could lead to useless complications, and I would stick to MVC (that could return JSON objects if required) if the AJAX calls are not that many.

TL;DR

If you need to build dynamic server-side web sites go for MVC. If you need to build rich web applications consumed by heterogeneous clients (SPAs, mobile apps, etc.) then go for Web API. At last, use both if you want to expose your services to independent external consumers but want to consume them yourself using less layers of abstraction.

More:

On The Coexistence of ASP.NET MVC and WebAPI

ASP.NET Web API vs. ASP.NET MVC “APIs”

MVC 5 Sharing between Web Application and Web API

To answer your questions:

  1. Yes, they need to be separate projects if you are going to deploy them seperately.
  2. To handle common items, create a library DLL project and reference that from both the Web API and Web App. It can contain the common components.

Also, if you are using cookies for authentication, you are also going to have to setup CORS to allow the cookie to be shared between two sites with different domains.

How to Call Web API from Another MVC Project with two Parameter one in header and second in body

You create a HttpClient object and set the header parameter

using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:64189/api/");

//your header parameter name and value
client.DefaultRequestHeaders.Add("hdrname", "hdrvalue");

//HTTP GET
var responseTask = client.GetAsync("youraction?param1=abc"); //Action Name
responseTask.Wait();

var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
// Use your class model to receive the data from the API
var readTask = result.Content.ReadAsAsync<IList<YourModel>>();
readTask.Wait();

var r = readTask.Result;
}
else //web api sent error response
{
//log response status here..
}
}


Related Topics



Leave a reply



Submit