ASP.NET MVC 6 中的 MVC 控制器和 Web API 控制器有什么区别? [英] What is the difference between MVC Controller and Web API Controller in ASP.NET MVC 6?

查看:12
本文介绍了ASP.NET MVC 6 中的 MVC 控制器和 Web API 控制器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP.NET 5 MVC 6 中,Microsoft 将普通 MVC 控制器类 (Controller) 与 Web Api 控制器类 (ApiController) 合并.现在只需要继承一个Controller类,它也包含了WebApi的特性.

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.

所以现在区分 MVC 和 WebApi 控制器并不那么简单.两者都继承自 Controller 类.我能发现的唯一区别是 WebApi 的路由信息​​仍然由属性 HttpGetHttpPostHttpPutHttpDelete 提供.但是现在可以使用属性路由对 MVC 控制器做同样的事情,只是使用不同的属性.

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.

甚至功能似乎已经合并.MVC 控制器现在也支持内容协商.

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

具体问题是:

还有真正的区别,还是只是指定路线的方式?现在哪种方式是创建 Web 应用程序的首选方式?

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?

(几乎)空的 MVC 控制器:

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

(几乎)为空的 WebApi 控制器:

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

如果您想尝试内容协商是否有效,则必须将此代码包含到您的 Startup.ConfigureServices 方法中,因为默认情况下返回类型仅为 JSON.

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

推荐答案

我觉得你想太多了.

您的第一个问题ASP.NET MVC 6 中的 MVC 控制器和 Web API 控制器有什么区别?"假设它们是不同的,但它们不是.它们是合并的,所以没有区别.

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.

如果您想定义单独的路由来封锁不返回 View 结果的操作方法,那么就去做吧.如何组织应用程序取决于您.询问现在哪种方式是创建 Web 应用程序的首选方式?"毫无意义,因为这取决于您的应用程序决定,并且在 MVC 6 投入生产使用很长一段时间之前,不会有更常见的处理方式.

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.

这篇关于ASP.NET MVC 6 中的 MVC 控制器和 Web API 控制器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆