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

后端 未结 2 542
独厮守ぢ
独厮守ぢ 2020-12-14 00:59

In ASP.NET 5 MVC 6 Microsoft merged the normal MVC controller class (Controller) with the Web Api controller class (ApiController). Now there is ju

相关标签:
2条回答
  • 2020-12-14 01:36

    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.

    0 讨论(0)
  • 2020-12-14 01:41

    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.

    0 讨论(0)
提交回复
热议问题