30

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();
});
1
  • 1
    There is no difference. The distinction only exist in the implementation of the controller actions. Commented Sep 2, 2015 at 13:10

2 Answers 2

23

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.

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

4 Comments

I thought about that too, there are still 2 different templates in Visual Studio. One for MVC Controller and one for WebApi Controller. So I concluded that there has to be a difference somewhere
@Dominik I think your conclusion is not fully thought out. The template is probably there for convenience, so that developers coming from an old version won't be lost when they want to Web API features into an MVC 6 app.
that makes sense. I'm still a bit confused why Microsoft would include 2 different mechanisms for the same thing. Its a new version after all, so they could just remove one way in favor of the other.
@Dominik Why shouldn't they include it? Templates are cheap and easy, and if it saves them from answering a lot of "Hey, Microsoft took away this template I used all the time" or "Why did Microsoft get rid of Web API" concerns, then it seems like a decent move to me.
10

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.

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.