·Î±×ÀÎ  È¸¿ø°¡ÀÔ

Top 10 Changes in ASP.NET 5 and MVC 6
ÃÖÁØ¿ø  (Homepage) 2015-03-06 14:31:26, Á¶È¸ : 25,382, Ãßõ : 6838

February 24th, 2015
  
Top 10 Changes in ASP.NET 5 and MVC 6

I spent the last couple of weeks writing sample code for ASP.NET 5/MVC 6 and I was surprised by the depth of the changes in the current beta release of ASP.NET 5. ASP.NET 5 is the most significant new release of ASP.NET in the history of the ASP.NET framework — it has been rewritten from the ground up.

In this blog post, I list what I consider to be the top 10 most significant changes in ASP.NET 5. This is a highly opinionated list. If other changes strike you as more significant, please describe the change in a comment.

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:
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" />
}

In the view above, the Html.BeginForm(), Html.LabelFor(), and Html.TextBoxFor() helpers are used to create the form. These helpers would not be familiar to an HTML designer.

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>


Notice that this new version of the form contains only (what looks like) HTML elements. For example, the form contains an INPUT element instead of an Html.TextBoxFor() helper. A front-end designer would be fine with this page.

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);
         }
     }
}


If the time is before 5:00pm then the View Component returns a partial named _Advertisement with the advertisement text ¡°Buy more coffee!¡±. If the time is after 5:00pm then the text changes to ¡°Buy more warm milk!¡±.

Here¡¯s what the _Advertisement partial looks like:

@model string

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


Finally, here is how you can use the BannerAd View Component in an MVC view:

@Component.Invoke("BannerAd")


View Components are very similar to subcontrollers. However, subcontrollers were always a little odd. They were pretending to be controller actions but they were not really controller actions. View Components just seem more natural.

6. GruntJS, NPM, and Bower Support

GruntJS

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.

AngularJS

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:

services.AddTransient<IRepository, EFRepository>();


After you bind EFRepository and IRepository then you can use constructor dependency injection in your MVC controllers (and any other class) using code like this:

public class ProductsController : Controller
{
     private IRepository _repo;

     public ProductsController(IRepository repo)
     {
         _repo = repo;
     }
}


In the code above, the IRepository interface is passed to the constructor for the ProductsController. The built-in ASP.NET Dependency Injection framework passes EFRepository to the ProductsController because IRepository was bound to EFRepository.

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:

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);

     }

}


ASP.NET 5 uses xUnit.net as its unit test framework. This framework uses the [Fact] attribute instead of the [TestMethod] attribute (and no [TestClass] attribute]):


public class CalculatorTests
{
     [Fact]
     public void AddNumbers()
     {
         // Arrange
         var calculator = new Calculator();

         // Act
         var result = calculator.AddNumbers(1, 1);

         // Assert
         Assert.Equal(result, 13);
     }
}


If you look at the source code for ASP.NET 5 then you¡¯ll see that xUnit.net is used to test ASP.NET extensively. For example, the MVC repository contains unit tests written with xUnit.net. You can take a look at the MVC repository (and its unit tests) here:

http://github.com/aspnet/mvc

ASP.NET uses a fork of xUnit.net that is located here:

https://github.com/aspnet/xunit




The source of (the) news  
http://stephenwalther.com/archive/2015/02/24/top-10-changes-in-asp-net-5-and-mvc-6



  ´ä±Û´Þ±â   ÃßõÇÏ±â   ¸ñ·Ïº¸±â

Copyright 1999-2024 Zeroboard / skin by zero