Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

c# - 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). 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();
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...