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

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

问题描述

在ASP.NET MVC 5 6微软与Web API控制器类(合并了正常的MVC控制器类(控制器 ApiController )。现在存在的仅仅是控制器类继承,其中包括的WebAPI的功能了。

所以现在还不是那么简单区分MVC和的WebAPI控制器。无论是从控制器类继承。我能发现的唯一区别是,的WebAPI的路由信息​​仍然由属性提供 HTTPGET HttpPost HttpPut HttpDelete 。但现在是可以做到的使用属性的路由,只是不同的属性MVC控制器相同。

即使是功能似乎已经合并。 MVC控制器现在支持内容协商过。

具体的问题是:

时还有一个真正的区别,还是只是被指定路线的方式吗?哪种方式现在是preferred来创建Web应用程序?

(几乎)空MVC控制器:

 公共类HomeController的:控制器
{
    公开名单<&人GT;指数()
    {
        返回新的List<&人GT;()
        {
            新的Person(){名字=测试1,姓氏=test2的},
            新的Person(){名字=TEST3,姓氏=TEST4}
        };
    }    公共IActionResult关于()
    {
        计算机[消息] =你的应用描述页面。        返回查看();
    }    公共IActionResult联系()
    {
        计算机[消息] =您的联系页面。        返回查看();
    }    公共IActionResult错误()
    {
        返回视图(〜/查看/共享/ Error.cshtml);
    }
}

(几乎)空的WebAPI控制器:

  [路线(API / [控制器])]
公共类Values​​Controller:控制器
{
    // GET:API /值
    [HTTPGET]
    公共IEnumerable的<&人GT;得到()
    {
        返回新的List<&人GT;()
        {
            新的Person(){名字=测试1,姓氏=test2的},
            新的Person(){名字=TEST3,姓氏=TEST4}
        };
    }    //获取API /价值/ 5
    [HTTPGET((编号))
    公共字符串GET(INT ID)
    {
        回到价值;
    }    // POST API /值
    [HttpPost]
    公共无效后([FromBody]字符串值)
    {
    }    // PUT API /价值/ 5
    [HttpPut((编号))
    公共无效认沽(INT ID,[FromBody]字符串值)
    {
    }    //删除API /价值/ 5
    [HttpDelete((编号))
    公共无效删除(INT ID)
    {
    }
}

编辑:

如果您想尝试,如果内容协商工作,你必须包括这code到Startup.ConfigureServices方法,因为每个默认返回类型为JSON只。

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


解决方案

我想你想进入这个太多了。

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

如果要定义不同的路线封锁动作方法不返回查看结果,然后去了。这取决于你如何组织你的应用程序。问:哪种方式现在是preferred来创建Web应用程序?是毫无意义的,因为这是由你来决定你的应用程序,而且也不会是做事,直到后MVC 6已经在生产中使用的时间长好了更常见的方式。

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

解决方案

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.

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

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