Key Changes in ASP.NET 5 and MVC 6

Original author: Stephen Walther
  • Transfer
Good morning, dear readers.

We will begin the week with such an informative, albeit somewhat abstract, translation that will help us clarify how interested the audience is in publishing ASP.NET books. Practice has shown that Peter's books " Application Development Using ASP.NET and AJAX " and " Programming Using Microsoft ASP.NET 4 " were not in great demand. However, it is quite possible that, based on the results of this post, we still decide to swipe at ASP.NET 5 and MVC 6.

Update as of April 30, 2015.

In the first edition of this post, I wrote that Microsoft does not plan to support VB.NET in ASP.NET 5 / MVC 6. Indeed, at first it was, but my post provoked a strong reaction in the VB.NET community. So, apparently, VB.NET again in the cage!

The last couple of weeks I spent writing code samples for ASP.NET 5 / MVC 6. I was just amazed at the depth of the 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 - we can say that the entire framework has been rewritten.

In this article, I will focus on 10 changes in ASP.NET 5 that seem to me the most significant. The list is very subjective. If you would like to mention any other changes - describe them in the comments.

1. ASP.NET on OSX and Linux

For the first time in ASP.NET history, ASP.NET 5 applications can run on OSX and Linux. I emphasize once again: ASP.NET 5 applications can run on Windows, OSX, and Linux. This fact opens ASP.NET for many developers and designers who previously could not use this framework.
The traditional ASP.NET audience is professional programmers working in large corporations. Clients of such corporations are tightly chained to their Windows computers.

The situation is completely different at startups, where it is customary to work with OSX / Linux. Whenever I get to a startup conference, it turns out that the whole audience is working with a Macbook Pro. Typically, these people do not work with ASP.NET.

In addition, designers and developers of the client side - at least those who work outside the corporate dungeons - also love working with the Macbook. I should be at a jQuery conference, MacBook Pros everywhere (the next picture is from the jQuery thematic blog).

Now that ASP.NET 5 will run on Windows, OSX and Linux, everything will change. For the first time, building applications using ASP.NET 5 is available to any developers and designers. Moreover, they can create in their favorite development environments, for example, in Sublime Text and WebStorm (Visual Studio is not required).

Get to know the OmniSharp project and see how you can use various text editors with ASP.NET 5 — specifically, Sublime Text, Atom, Emacs, and Brackets:

2. No more web forms

I love ASP.NET web forms . Over the course of my life, I have spent hundreds - if not thousands - of hours programming applications with Web Forms. However, now is the time to say goodbye to them. ASP.NET 5 will no longer have web forms.

Applications with Web Forms can continue to be programmed in Visual Studio 2015 by specifying the .NET 4.6 framework as the target platform. However, web form applications do not allow you to work with any of the new cool ASP.NET 5 features described in this list. If you don’t want to stay on the sidelines of historical progress, now is the time to finally rewrite your application from Web Forms to ASP.NET MVC.

3. Tag Helpers

Tag Helpers (helpers) are probably the very possibility that can most strongly affect the views you create in an ASP.NET MVC application. Tag Helpers is 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())
@Html.LabelFor(m => p.Name, "Name:") @Html.TextBoxFor(m => p.Name)

In the above example, the Html.BeginForm () Html.LabelFor () and Html.TextBoxFor () helpers are used to create the form. These helpers are unlikely to have been encountered by an HTML designer.
Here's how exactly the same form is created using Tag Helpers:

@model MyProject.Models.Product
@addtaghelper "Microsoft.AspNet.Mvc.TagHelpers"

Please note: this updated version of the form contains only HTML elements (at least it seems so at first glance). For example, the form has an INPUT element, not a Html.TextBoxFor () helper. The client side designer would have liked this page. All the intricacies inherent in this view are associated with its asp-for attributes. These attributes are used to complement elements with ASP.NET MVC server functionality.
In the next article, Damien Edwards demonstrates a whole site that uses nothing but Tag Helpers:


View Components Farewell sub-controllers, long live View Components!
In previous versions of ASP.NET MVC, we used the Html.Action () helper to call the subcontroller. Suppose you want to display an advertising banner in several views at once. In this case, it would be necessary to create a subcontroller that contained the logic for returning a specific banner, and the subcontroller would fire as a result of calling the Html.Action () helper from the view.
Subcontrollers - the Html.Action () helper - are absent in the current beta version of MVC 6. Instead, MVC 6 uses an alternative technology called View Components.

Here's how to create a presentation component that displays one of two banner ads based 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);

Between 5 p.m. the presentation component returns a partial template called _Advertisement with the ad text “Buy more coffee!”. In the period after 17.00, this text changes to “Buy more warm milk!”.
Here's what a partial _Advertisement template looks like:

@model string

Finally, here's how to use the BannerAd view component in the MVC view:


Representation 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. Components of representations look much more natural.

5. GruntJS, NPM, and Bower Support

Client side developers will genuinely love ASP.NET 5 because GruntJS (and, ultimately, Gulp) is supported in this framework.

GruntJS is a task manager that allows you to collect resources for the client side, for example, JavaScript and CSS files. For example, GruntJS can be used to concatenate and reduce JavaScript files during each build in Visual Studio.

There are thousands of GruntJS plugins that allow you to solve an unimaginable number of tasks (currently there are 4,334 such modules 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 in CSS, TypeScript to JavaScript compilation and image reduction.
To provide support for GruntJS, Microsoft needed to support two new package managers (except NuGet). First, since GruntJS plugins are distributed as NPM packages, Microsoft has added support for NPM packages.

Secondly, since many client resources, in particular Twitter Bootstrap, jQuery, Polymer, and AngularJS, are distributed through Bower, Microsoft has added support for Bower.

Thus, you can run GruntJS using plugins from NPM, and client resources from Bower.

6. Unified MVCs and Web APIs

In previous versions of ASP.NET MVCs, MVCs were different from Web APIs. The MVC controller used the base class System.Web.MVC.Controller, and the Web API controller used the base class System.Web.Http.ApiController.

There is only one Controller class left in MVC 6, which is the base class for all MVC controllers and the Web API. This is a Microsoft.AspNet.Mvc.Controller class.

MVC 6 controllers return IActionResult interfaces. When used as an MVC controller, IActionResult can be a view. In the case of the Web API controller, IActionResult may be data (for example, a list of products). The same controller may have actions that return both views and data.

In MVC 6, both MVC controllers and Web API controllers use the same routes. You can use either contracted routes or attribute-based routes. They apply to all controllers in the project.

7. AngularJS

AngularJS is one of the most popular single-page application (SPA) client frameworks. Visual Studio 2015 has templates for creating AngularJS modules, controllers, directives, and factories.

With support for GruntJS in ASP.NET 5, ASP.NET is a great server-side framework for building AngularJS client applications. You can automatically combine and reduce all of your AngularJS files during each build. You can interact with the MVC 6 controller from $ resource AngularJS using REST.

8. Dependency injection framework in ASP.NET

ASP.NET 5 has built-in support for dependency injection and the "service locator" pattern. Thus, you no longer depend on third-party frameworks for dependency injection, for example, on Ninject or AutoFac.
Suppose you created an IRepository interface and an EFRepository class that implements this interface. In this case, you can bind the EFRepository class to the IRepository interface using the ConfigureServices () method of the Startup.cs class like this:


By linking EFRepository and IRepository, you can embed the constructor dependency in your MVC controllers (and any other class) using 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 dependency injection framework in ASP.NET passes the EFRepository to the ProductsController since the IRepository interface has been associated with the EFRepository.

In addition, you can use the "Service Locator" pattern. Whenever it is possible to access the HttpContext, you can access any registered services. For example, you can extract an EFRepository by applying the following code instead of the MVC action controller:

var repo = this.Context.ApplicationServices.GetRequiredService();


Goodbye Visual Studio Unit Testing Framework, long live!
In previous versions of ASP.NET MVC, the Visual Studio Unit Testing Framework (sometimes called mstest) was used by default for testing. To describe the unit test, it uses the attributes [TestClass] and [TestMethod]:

public class CalculatorTests {
    public void TestAddNumbers() {
        // Условие
        var calc = new Calculator();
        // Действие
        var result = calc.AddNumbers(0, 0);
        // Результат
        Assert.AreEqual(0, result);

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

public class CalculatorTests
    public void AddNumbers()
        // Условие
        var calculator = new Calculator();
        // Действие
        var result = calculator.AddNumbers(1, 1);
        // Результат
        Assert.Equal(result, 13);

By working with the ASP.NET 5 source code, you will see that is used for extensive ASP.NET testing. For example, in the MVC repository there are unit tests written using The MVC repository (and its unit tests) can be found here:

ASP.NET uses the branch located here:

Also popular now: