Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015: MVC 6 Scaffolding switched to using IActionResult intead of ActionResult

I've just installed VS 2015 and I noticed a few changes in the auto-scaffolding of MVC6. I am curious why Microsoft made those changes, as I think if they decided to do some there might be some benefits that I may not know of.

In VS 2013, the MVC 5 Auto-Scaffolding always used: ActionResult

In VS 2015 the MVC 6 Auto-Scaffolding switched to using: IActionResult

In VS 2015 I notice that the Microsoft team prefer not to do this anymore:

 public class Test{
     private int i;
     public Test (int i){
         this.i = i;
     }
 }

While in all generated classes I saw that they did:

 public class Test{
     private int _i;
     public Test (int i){
         _i = i;
     }
 }

If it is just the matter of coding style, it's fine I will immediately lose my interests in known why they changed this, but if there is any logical explanation behind this I can't wait to know what that is.

like image 848
Arnold Zahrneinder Avatar asked Oct 20 '22 05:10

Arnold Zahrneinder


1 Answers

As far as your ActionResult question is concerned, in previous ASP.NET, MVC controllers used the System.Web.MVC.Controller Parent class and a Web API controller used the System.Web.Http.ApiController Parent class.

But in ASP.NET 5 MVC 6, they have merged both in a single web app. So now there is only 1 controller class Microsoft.AspNet.Mvc.Controller class as a base for both of them. Now to distinguish bethween them, when used as an MVC controller, the IActionResult might be a view. When used as a Web API controller, the IActionResult might be data (JSON/XML). The same controller might have actions that return both views and data.

like image 112
Awais Mahmood Avatar answered Oct 22 '22 00:10

Awais Mahmood