3

I'm a bit confused. I'm on my way to learn ASP.net MVC, currentlyin Version 3. I was already irritated when I learned that WebAPI is the newest Web-Technology from Microsoft and asked myself if I should continue with MVC.

From my research today I understand, that WebAPI is for solutions that require pure Http based APIs which serve data to other applications than browsers. And that it has no HTML rendering engine. So for solutions that have to render a lot of HTML pages MVC is fine.

I also read that Web APIs development that was made in parallel to MVC and stands for it own.

No today when starting Visual Studio the first time I read that one of the new features of MVC4 is WebAPI:

http://www.asp.net/mvc/mvc4

Is Web API now an integral part of MVC? Can MVC Controllers directly mixed with Web API controllers in the same application? In one blog post I read that if you use WebAPI and MVC in one project you will end up with duplicating code. Is this still the case?

Best Thomas

2
  • I read that one of the new features of MVC4 is WebAPI. Is that not answering your own question? :| Maybe the title should reflect one of your sub questions that you didn't already know. (can they be mixed?) Commented Nov 23, 2012 at 16:07
  • 1
    Two months ago, MVC and Web API where displayed as alternatives besides classic ASP.net on the ASP.net home page. Commented Nov 23, 2012 at 16:19

4 Answers 4

1

Web API is bundled with MVC 4. It can be used separately and in combination with MVC. There are some rough edges around the integration but nothing very painful.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure "Web API is bundled with MVC 4" are the right words here.
1

The answer is YES! WebAPI has both the 'Model' and the 'Controller' from the MVC design pattern. But where is the 'View'?

If you create a new WebAPI project, visual studio will create a default HomeController which is ASP.NET MVC standard controller and it will also add a Index action with a Index.cshtml view. It works this way so that you will be able to host your WebAPI services in order to test them. In WCF, visual studio launched the WCF Test client for that purpose.

If there was no view there was no way to test our services without hosting them on IIS.

WebAPI also uses the same routing engine and many more server side features like ASP.NET MVC.

Comments

0

ASP.NET Web API has nothing to do with ASP.NET MVC. The team has put ASP.NET Web API inside the same MSI installer with ASP.NET MVC 4 which is a mistake IMHO. This is where all the confusion begins but the team is aware of that confusion AFAIK.

ASP.NET Web API stands on its own and it doesn't even have any dependency directly on System.Web. Have a look at this post:

ASP.NET Web API does not Sit on Top of ASP.NET MVC! In Fact, It does not Sit on Top of Anything

Comments

0

ASP.NET MVC

public class TweetsController : Controller {
  // GET: /Tweets/
  [HttpGet]
  public ActionResult Index() {
    return Json(Twitter.GetTweets(), JsonRequestBehavior.AllowGet);
  }
}

ASP.NET Web API

public class TweetsController : ApiController {
  // GET: /Api/Tweets/
  public List<Tweet> Get() {
    return Twitter.GetTweets();
  }
}

Code comes from this link.

There is a lot of similarity in both technologies, e.g. servicing HTTP request. The most important difference is that:

  • MVC creates HTML views,
  • WebAPI has built-in serialization.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.