Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In ASP.NET 5 MVC 6 Microsoft merged the normal MVC controller class (Controller) with the Web Api controller class (ApiController). Now there is just a Controller class to inherit from, which includes the features of WebApi too.

So now it is not as simple to distinguish MVC and WebApi controllers. Both inherit from the Controller class. The only difference I can spot is that the routing information of WebApi is still provided by the attributes HttpGet, HttpPost, HttpPut and HttpDelete. But now it is possible to do the same with MVC controllers using attribute routing, just with different attributes.

Even the features seem to have merged. MVC controllers support now content negotiation too.

The concrete questions are:

Is there still a real difference, or is it just the way the routes are specified? Which way is now the preferred one to create web apps?

(Almost) empty MVC controller:

public class HomeController : Controller {     public List<Person> Index()     {         return new List<Person>()         {             new Person() {Firstname = "test1", Lastname = "test2"},             new Person() {Firstname = "test3", Lastname = "test4"}         };     }      public IActionResult About()     {         ViewData["Message"] = "Your application description page.";          return View();     }      public IActionResult Contact()     {         ViewData["Message"] = "Your contact page.";          return View();     }      public IActionResult Error()     {         return View("~/Views/Shared/Error.cshtml");     } } 

(Almost) empty WebApi controller:

[Route("api/[controller]")] public class ValuesController : Controller {     // GET: api/values     [HttpGet]     public IEnumerable<Person> Get()     {         return new List<Person>()         {             new Person() {Firstname = "test1", Lastname = "test2"},             new Person() {Firstname = "test3", Lastname = "test4"}         };     }      // GET api/values/5     [HttpGet("{id}")]     public string Get(int id)     {         return "value";     }      // POST api/values     [HttpPost]     public void Post([FromBody]string value)     {     }      // PUT api/values/5     [HttpPut("{id}")]     public void Put(int id, [FromBody]string value)     {     }      // DELETE api/values/5     [HttpDelete("{id}")]     public void Delete(int id)     {     } } 

EDIT:

If you want to try if content negotiation works, you have to include this code into your Startup.ConfigureServices method, because per default the return type is JSON only.

services.Configure<MvcOptions>(options => {     options.AddXmlDataContractSerializerFormatter(); }); 
like image 684
Domysee Avatar asked Sep 02 '15 12:09

Domysee


1 Answers

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.

like image 90
mason Avatar answered Oct 06 '22 01:10

mason