简体   繁体   English

ASP.NET MVC 6中的MVC控制器和Web API控制器有什么区别?

[英]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 ). 在ASP.NET 5 MVC 6中,Microsoft将普通的MVC控制器类( Controller )与Web Api控制器类( ApiControllerApiController Now there is just a Controller class to inherit from, which includes the features of WebApi too. 现在只有一个继承的Controller类,其中也包含了WebApi的功能。

So now it is not as simple to distinguish MVC and WebApi controllers. 所以现在区分MVC和WebApi控制器并不是那么简单。 Both inherit from the Controller class. 两者都继承自Controller类。 The only difference I can spot is that the routing information of WebApi is still provided by the attributes HttpGet , HttpPost , HttpPut and HttpDelete . 我能发现的唯一区别是WebApi的路由信息​​仍然由属性HttpGetHttpPostHttpPutHttpDelete But now it is possible to do the same with MVC controllers using attribute routing, just with different attributes. 但现在可以使用属性路由对MVC控制器执行相同操作,只需使用不同的属性。

Even the features seem to have merged. 即使这些功能似乎也已合并。 MVC controllers support now content negotiation too. MVC控制器现在也支持内容协商。

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: (差不多)空的MVC控制器:

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: (差不多)空的WebApi控制器:

[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. 如果要尝试内容协商,则必须将此代码包含在Startup.ConfigureServices方法中,因为默认情况下返回类型仅为JSON。

services.Configure<MvcOptions>(options =>
{
    options.AddXmlDataContractSerializerFormatter();
});

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?" 您的第一个问题“ASP.NET MVC 6中MVC控制器和Web API控制器的区别是什么?” 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. 如果要定义单独的路由以封锁不返回View结果的操作方法,请继续操作。 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. 这是没有意义的,因为由您来决定您的应用程序,并且在MVC 6长时间投入生产之前,不会有更常见的做事方式。

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 . Microsoft将ApiControllerController合并为一个类Controller In order to do that, they removed some features of the ApiController . 为了做到这一点,他们删除了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. 例如,不是将HTTP Action指定为参数方法的前缀和路由属性中的路由,而是使用HttpGetHttpPost属性完成两者。

[HttpGet("api/visits")]

If you want to migrate from WebApi project, here is some guidance to do that. 如果您想从WebApi项目迁移, 这里有一些指导。

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 . 如果您不想迁移,只是想将项目转换为新的ASP.NET MVC版本,则可以使用Web API Compatibility Shim Here and here you find guidance for that. 在这里这里你可以找到指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM