Home .NET Major changes in ASP.NET 5 and MVC 6

Major changes in ASP.NET 5 and MVC 6

by admin

Good morning, dear readers.
We’ll start the week with this informative, albeit somewhat thesaurus translation, which will help us clarify how interested the audience is in publishing books on ASP.NET. Practice has shown that Peter’s books " Application development using ASP.NET and AJAX " and " Programming using Microsoft ASP.NET 4 " have not been in great demand. However, it’s possible that based on the results of this post, we may still decide to take a swing at ASP.NET 5 and MVC 6.
Update April 30, 2015.
In the first edition of this post, I wrote that Microsoft had no plans to support VB.NET in ASP.NET 5/MVC 6. Indeed, at first it was, but my post caused a strong reaction in the VB.NET community. So, apparently, VB.NET is back in the fold!
I’ve spent the last couple of weeks writing sample code for ASP.NET 5/MVC 6. I’ve been simply amazed at the depth of changes made to the beta version of ASP.NET 5, current at the moment. ASP.NET 5 is the most significant new release in the history of the entire ASP.NET framework – you could say that the entire framework has been rewritten.
In this article, I will focus on the 10 changes in ASP.NET 5 that seem to me to be the most significant. The list is very subjective. If there are other changes you would like to mention, write them in the comments.
1. ASP.NET in OSX and Linux
Major changes in ASP.NET 5 and MVC 6
For the first time in the history of ASP.NET, ASP.NET 5 applications can run on OSX and Linux operating systems. Once again: ASP.NET 5 applications can run on Windows, OSX and Linux. This fact opens ASP.NET to many developers and designers who previously could not use this framework.
The traditional audience for ASP.NET is professional programmers working for large corporations. The clients of such corporations are chained to their Windows computers.
The situation is completely different at startups, where it is common to work with OSX/Linux. Whenever I get to a startup conference, it turns out that the whole audience works with Macbook Pro. As a rule, these people don’t work with ASP.NET.
In addition, designers and client-side developers-at least those who work outside the corporate walls-also love working with Macbooks. Every time I go to a jQuery conference, there are Macbook Pro’s everywhere (the following picture is taken from a jQuery theme blog).
Now that ASP.NET 5 will run on Windows, OSX and Linux, everything will change. For the first time, building applications with ASP.NET 5 is available to all developers and designers. And they can create in their favorite development environments, such as Sublime Text and WebStorm (Visual Studio is not required).
Take a look at the OmniSharp project and see how different text editors – particularly Sublime Text, Atom, Emacs, and Brackets – can be used with ASP.NET 5:
2. No more web forms
I love ASP.NET Web Forms. I’ve spent hundreds – if not thousands – of hours programming Web Forms applications throughout my life. Now, however, it’s time to say goodbye to them. There will be no more Web Forms in ASP.NET 5.
Web Forms applications can continue to be programmed in Visual Studio 2015 by specifying the .NET 4.6 framework as the target platform. However, Web Forms applications don’t allow you to work with any of the new ASP.NET 5 class features described in this list. If you don’t want to be left on the sidelines of historical progress, now is the time to finally rewrite your application from Web Forms to ASP.NET MVC.
3. Day Helpers
Tag Helpers are probably the feature that can have the most impact on the views you create in an ASP.NET MVC application. Tag Helpers are a more convenient alternative than traditional MVC helpers.
Consider the following MVC view, which contains a form for creating a new product :

@model MyProject.Models.Product@using (Html.BeginForm()){<div>@Html.LabelFor(m => p.Name, "Name:")@Html.TextBoxFor(m => p.Name)</div><input type="submit" value="Create" />}

In the example above, the Html.BeginForm() Html.LabelFor() and Html.TextBoxFor() helpers are used to create a form. These helpers are unlikely to be encountered by the HTML designer.
Here’s how the exact same form is created with Tag Helpers:

@model MyProject.Models.Product@addtaghelper "Microsoft.AspNet.Mvc.TagHelpers"<form asp-controller="Products" asp-action="Create" method="post"><div><label asp-for="Name"> Name:</label><input asp-for="Name" /></div><input type="submit" value="Save" /></form>

Note : this updated version of the form contains only HTML elements (at least it seems that way at first glance). For example, the form has an INPUT element, not the Html.TextBoxFor() helper. The client-side designer would love this page. All of the subtleties inherent in this view have to do with its asp-for attributes. These attributes are used to augment the elements with ASP.NET MVC server functionality.
In the following article, Damien Edwards demonstrates an entire site that uses nothing but Tag Helpers:
4. Components of representations
Goodbye subcontrollers, hello View Components!
In previous versions of ASP.NET MVC, we used the Html.Action() helper to call a subcontroller. Suppose you want to display an advertising banner in several views at once. In this case you would need to create a subcontroller which would contain the logic to return a specific banner, and the subcontroller would be triggered by calling the Html.Action() helper from the view.
Subcontrollers, the Html.Action() helper, are not present in the current beta version of MVC 6. Instead, MVC 6 uses an alternative technology called View Components.
Here’s how to create a view component that displays one of two ad banners depending on the time of day :

using Microsoft.AspNet.Mvc;using System;namespace Partials.Components{public class BannerAd : ViewComponent{public IViewComponentResult Invoke(){var adText = "Buy more coffee!";if (DateTime.Now.Hour > 18){adText = "Buy more warm milk!";}return View("_Advertisement", adText);}}}

Before 5pm, the view component returns a partial template called _Advertisement with the ad text "Buy more coffee!". After 5pm, this text changes to "Buy more warm milk!".
This is what a partial _Advertisement template looks like:

@model string<div style="border:2px solid green;padding:15px">@Model</div>

Finally, here’s how you can use the BannerAd view component in the MVc view :


The view components are very similar to subcontrollers. However, subcontrollers have always been weird. They were supposed to work as action controllers, but in practice they were not. The view components look much more natural.
5. GruntJS, NPM and Bower Support
Major changes in ASP.NET 5 and MVC 6
Client-side developers will sincerely love ASP.NET 5 due to the fact that this framework supports GruntJS (and eventually Gulp).
GruntJS is a task manager that allows you to build resources for the client side, such as JavaScript and CSS files. For example, you can use GruntJS to concatenate and reduce JavaScript files when you run each build in Visual Studio.
There are thousands of GruntJS plugins that allow you to solve an unimaginable number of tasks (there are currently 4334 such plugins in the GruntJS plugin repository):
For example, here you will find plugins for running JavaScript unit tests, validating JavaScript code quality (jshint), compiling LESS and Sass files into CSS, compiling TypeScript into JavaScript and reducing images.
To provide support for GruntJS, Microsoft needed to support two new package managers (besides NuGet). First, because GruntJS plugins are distributed as NPM packages, Microsoft added support for NPM packages.
Secondly, because many client-side resources – notably Twitter Bootstrap, jQuery, Polymer, and AngularJS- are distributed via Bower, Microsoft has added support for Bower.
So you can run GruntJS with plugins from NPM and client resources from Bower.
6. Unified MVC controllers and Web APIs
In previous versions of ASP.NET MVC, MVC controllers were different from Web API controllers. The MVC controller used the System.Web.MVC.Controller base class, and the Web API controller used the System.Web.Http.ApiController base class.
In MVC 6 there is only one Controller class left, which is the base class for all MVC controllers and Web APIs. We are talking about Microsoft.AspNet.Mvc.Controller.
MVC 6 controllers return IActionResult interfaces. When used as an MVC controller, IActionResult can be a view. In the case of a Web API controller, IActionResult can be data (such as a list of products). The same controller can have actions that return both views and data.
In MVC 6, both MVC controllers and Web API controllers use the same routes. You can apply either agreement-based routes or attribute-based routes. They apply to all controllers in the project.
7. AngularJS
AngularJS is one of the most popular client-side frameworks for creating single-page applications (SPAs). Visual Studio 2015 has templates for creating AngularJS modules, controllers, directives, and factories.
With GruntJS support in ASP.NET 5, ASP.NET turns out to be a great server-side framework for building AngularJS client applications. You can automatically combine and shrink all your AngularJS files at each build. You can communicate with the MVC 6 controller from $resource AngularJS using REST.
8. Dependency implementation framework in ASP.NET
ASP.NET 5 has built-in support for dependency enforcement and the "service locator" pattern. Thus, you no longer depend on third-party frameworks for implementing dependencies, such as Ninject or AutoFac.
Suppose you have created an IRepository interface and an EFRepository class that implements that interface. In that case, you can bind the EFRepository class to the IRepository interface using the ConfigureServices() method of the Startup.cs class like this :

services.AddTransient<IRepository, EFRepository> ();

By linking EFRepository and IRepository, you can implement the constructor dependency in your MVC controllers (and any other class) with similar code :

public class ProductsController : Controller{private IRepository _repo;public ProductsController(IRepository repo){_repo = repo;}}

In the above code, the IRepository interface is passed to the constructor for the ProductsController. This built-in framework for implementing dependencies in ASP.NET passes EFRepository to ProductsController because the IRepository interface was associated with EFRepository.
You can also use the "Service Locator" pattern. Whenever it is possible to access the HttpContext, you can also access any registered services. For example, you can extract EFRepository by applying the following code instead of the MVC action controller:

var repo = this.Context.ApplicationServices.GetRequiredService<IRepository> ();

9. xUnit.net
Goodbye Visual Studio Unit Testing Framework, long live xUnit.net!
Previous versions of ASP.NET MVC used the Visual Studio Unit Testing Framework (sometimes called mstest) for testing by default. It used the [TestClass] and [TestMethod] attributes to describe a unit test:

[TestClass]public class CalculatorTests {[TestMethod]public void TestAddNumbers() {// Conditionvar calc = new Calculator();// Actionvar result = calc.AddNumbers(0, 0);// ResultAssert.AreEqual(0, result);}}

ASP.NET 5 uses the xUnit.net framework for unit testing. It uses the [Fact] attribute instead of [TestMethod], and the [TestClass] attribute is missing :

public class CalculatorTests{[Fact]public void addNumbers(){// Conditionvar calculator = new Calculator();// Actionvar result = calculator.AddNumbers(1, 1);// ResultAssert.Equal(result, 13);}}

After working with the ASP.NET 5 source code, you will see that xUnit.net is used for extensive ASP.NET testing. For example, the MVC repository has unit tests written using xUnit.net. The MVC repository (and its unit tests) can be found here :
ASP.NET uses the xUnit.net branch located here :

You may also like