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

23,856

Solution 1

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.

Solution 2

While mason answered the question perfectly, I want to provide some additional information on the differences and some resources that hopefully will help future visitors of the question.

Microsoft merged ApiController and Controller into one class, Controller. In order to do that, they removed some features of the ApiController.

This is a great blog post describing the changes.

For example, instead of specifying the HTTP Action as prefix of the parameter method and the route in a route attribute, both are now done with the HttpGet and HttpPost attributes.

[HttpGet("api/visits")]

If you want to migrate from WebApi project, here is some guidance to do that.

If you dont want to migrate, but simply want to convert the project to the new ASP.NET MVC version, you can use the Web API Compatibility Shim. Here and here you find guidance for that.

Share:
23,856
Domysee
Author by

Domysee

Engineering Manager at Dynatrace.

Updated on July 24, 2022

Comments

  • Domysee
    Domysee almost 2 years

    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();
    });