Top 10 Changes in ASP.NET 5 and MVC 6
1. ASP.NET on OSX and Linux
For the first time in the history of ASP.NET, you can run ASP.NET 5 applications on OSX and Linux. Let me repeat this. ASP.NET 5 apps can run on Windows, OSX, and Linux. This fact opens up ASP.NET to a whole new audience of developers and designers.
The traditional audience for ASP.NET is professional developers working in a corporation. Corporate customers are welded to their Windows machines.
Startups, in stark contrast, tend to use OSX/Linux. Whenever I attend a startup conference, the only machines that I see in the audience are Macbook Pros. These people are not the traditional users of ASP.NET.
Furthermore, designers and front-end developers – at least when they are outside the corporate prison – also tend to use Macbook Pros. Whenever I attend a jQuery conference, I see Macbook Pros everywhere (the following picture is from the jQuery blog).
Enabling ASP.NET 5 to run on Windows, OSX, and Linux changes everything. For the first time, all developers and designers can start building apps with ASP.NET 5. And, they can use their favorite development environments such as Sublime Text and WebStorm when working with ASP.NET apps (No Visual Studio required).
Take a look at the OmniSharp project to see how you can use editors such as Sublime Text, Atom, Emacs, and Brackets with ASP.NET 5:
http://www.omnisharp.net/
2. No More Web Forms
I love ASP.NET Web Forms. I’ve spent hundreds – if not thousands – of hours of my life building Web Forms applications. However, it is finally time to say goodbye. ASP.NET Web Forms is not part of ASP.NET 5.You can continue to build Web Forms apps in Visual Studio 2015 by targeting the .NET 4.6 framework. However, Web Forms apps cannot take advantage of any of the cool new features of ASP.NET 5 described in this list. If you don’t want to be left behind as history marches forward then it is finally time for you to rewrite your Web Forms app into ASP.NET MVC.
3. No More Visual Basic
It is also time to say goodbye to Visual Basic. ASP.NET 5 only supports C# and Visual Basic is left behind.
My hope is that this change won’t be too painful. I believe that there are only two people in the entire world who are building MVC apps in Visual Basic. It is time for both of you to stop it. There are good automatic converters for going from Visual Basic to C#:
http://converter.telerik.com/
4. Tag Helpers
Tag Helpers is the one feature that might have the biggest impact on the way that you create your views in an ASP.NET MVC application. Tag Helpers are a better alternative to using traditional MVC helpers.Consider the following MVC view that 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" /> } |
Here’s how the exact same form can be created by using 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 > |
The only thing special about this view is the special asp-for attributes. These attributes are used to extend the elements with server-side ASP.NET MVC functionality.
Damien Edwards put together an entire sample site that uses nothing but Tag Helpers here:
https://github.com/DamianEdwards/TagHelperStarterWeb
5. View Components
Goodbye subcontrollers and hello View Components!In previous versions of ASP.NET MVC, you used the Html.Action() helper to invoke a subcontroller. For example, imagine that you want to display banner ads in multiple views. In that case, you would create a subcontroller that contained the logic for returning a particular banner advertisement and call the subcontroller by invoking Html.Action() from a view.
Subcontrollers – the Html.Action() helper — are not included in the current beta of MVC 6. Instead, MVC 6 includes an alternative technology called View Components.
Here’s how you can create a View Component that displays one of two banner advertisements 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); } } } |
Here’s what the _Advertisement partial looks like:
@model string < div style = "border:2px solid green;padding:15px" > @Model </ div > |
1
| @Component.Invoke("BannerAd") |
6. GruntJS, NPM, and Bower Support
Front-end development gets a lot of love in ASP.NET 5 through its support for GruntJS (and eventually Gulp).
GruntJS is a task runner that enables you to build front-end resources such as JavaScript and CSS files. For example, you can use GruntJS to concatenate and minify your JavaScript files whenever you perform a build in Visual Studio.
There are thousands of GruntJS plugins that enable you to do an amazing variety of different tasks (there are currently 4,334 plugins listed in the GruntJS plugin repository):
http://gruntjs.com/plugins
For example, there are plugins for running JavaScript unit tests, for validating the code quality of your JavaScript (jshint), compiling LESS and Sass files into CSS, compiling TypeScript into JavaScript, and minifying images.
In order to support GruntJS, Microsoft needed to support two new package managers (beyond NuGet). First, because GruntJS plugins are distributed as NPM packages, Microsoft added support for NPM packages.
Second, because many client-side resources – such as Twitter Bootstrap, jQuery, Polymer, and AngularJS – are distributed through Bower, Microsoft added support for Bower.
This means that you can run GruntJS using plugins from NPM and client resources from Bower.
7. Unified MVC and Web API Controllers
In previous versions of ASP.NET MVC, MVC controllers were different than Web API controllers. An MVC controller used the System.Web.MVC.Controller base class and a Web API controller used the System.Web.Http.ApiController base class.In MVC 6, there is one and only one Controller class that is the base class for both MVC and Web API controllers. There is only the Microsoft.AspNet.Mvc.Controller class.
MVC 6 controllers return an IActionResult. When used as an MVC controller, the IActionResult might be a view. When used as a Web API controller, the IActionResult might be data (such as a list of products). The same controller might 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 convention-based routes or attribute routes and they apply to all controllers in a project.
8. AngularJS
AngularJS is one of the most popular client-side frameworks for building Single Page Applications (SPAs). Visual Studio 2015 includes templates for creating AngularJS modules, controllers, directives, and factories.The support in ASP.NET 5 for GruntJS makes ASP.NET an excellent server-side framework for building client-side AngularJS apps. You can combine and minify all of your AngularJS files automatically whenever you perform a build. You can interact with an MVC 6 controller from an AngularJS $resource using REST.
9. ASP.NET Dependency Injection Framework
ASP.NET 5 has built-in support for Dependency Injection and the Service Locator pattern. This means that you no longer need to rely on third-party Dependency Injection frameworks such as Ninject or AutoFac.Imagine, for example, that 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 in the ConfigureServices() method of the Startup.cs class like this:
1
| services.AddTransient<IRepository, EFRepository>(); |
public class ProductsController : Controller { private IRepository _repo; public ProductsController(IRepository repo) { _repo = repo; } } |
You also can use the Service Locator pattern. Wherever you can access the HttpContext, you can access any registered services. For example, you can retrieve the EFRepository by using the following code inside of an MVC controller action:
1
| var repo = this.Context.ApplicationServices.GetRequiredService<IRepository>(); |
10. xUnit.net
Goodbye Visual Studio Unit Testing Framework and hello xUnit.net!In previous versions of ASP.NET MVC, the default testing framework was the Visual Studio Unit Testing Framework (sometimes called mstest). This framework uses the [TestClass] and [TestMethod] attributes to describe a unit test:
[TestClass] public class CalculatorTests { [TestMethod] public void TestAddNumbers() { // Arrange var calc = new Calculator(); // Act var result = calc.AddNumbers(0, 0); // Assert Assert.AreEqual(0, result); } } |
public class CalculatorTests { [Fact] public void AddNumbers() { // Arrange var calculator = new Calculator(); // Act var result = calculator.AddNumbers(1, 1); // Assert Assert.Equal(result, 13); } } |
http://github.com/aspnet/mvc
ASP.NET uses a fork of xUnit.net that is located here:
https://github.com/aspnet/xunit
-
Stephen walther
Comments
Post a Comment