Top 10 Features of ASP.NET Core for ASP.NET MVC Developers

WEBINAR:On-Demand

Introduction

ASP.NET Core 1.0 (formerly ASP.NET 5) provides a revamped Web development framework geared towards the requirements of modern Web applications. The new framework, currently in RC1, requires you to learn many new concepts not found in ASP.NET MVC 5. To that end, this article enumerates a few important features that ASP.NET MVC 5 developers should know as they prepare to learn this new framework.

Note: This article is based on RC1 of ASP.NET Core 1.0. You can check the roadmap and the availability of latest releases of the framework here.

1. ASP.NET Core on Multiple Platforms

ASP.NET and the .NET framework are targeted towards the Windows platform. On the other hand, ASP.NET Core is developed to support multiple platforms including Windows, Mac, and Linux. ASP.NET Core is a part of .NET Core—a new modular framework that supports multiple platforms. This also means that, unlike ASP.NET web applications, primarily run under IIS, the ASP.NET Core applications can run under non-IIS Web servers. Figure 1 shows the role of the .NET Core and ASP.NET Core.

Figure 1: The role of the .NET Core and ASP.NET Core

As you can see, a Web application built with ASP.NET Core can target .NET Framework 4.6 or the .NET Core. The Web applications targeting .NET Framework 4.6 run only on the Windows platform. The Web applications targeting the .NET Core can run on Windows and non-Windows platforms. Of course, as on this writing .NET Core doesn't offer the same rich functionality offered by .NET Framework 4.6.

Notice that, to develop Web applications using ASP.NET Core, you use ASP.NET Core MVC. Of course, additional frameworks (such as EF Core) and packages can be added as per your requirement.

2. Role of Project.json

ASP.NET Core uses a special file—Project.json—for storing all the project-level configuration information. Project.config can store many configuration settings, such as references to NuGet packages used in the project and target frameworks. For a complete list, you may go here.

Just to give you an idea as to how Project.json is structured, have a look at the following markup from a sample Project.json.

The Project.json file stores configuration information in JSON format. The above markup shows a dependencies section that contains a list of NuGet packages required by the application. For example, the Web application under consideration requires the 6.0.0-rc1-final version of Microsoft.AspNet.Mvc assembly, and so on. Some other commonly used sections of Project.json are frameworks, commands, and sources.

3. Role of AppSettings.json

ASP.NET stores application configuration settings in Web.config. For example, developers use the <appSettings> section to store custom application settings, the <connectionStrings> section to store database connection strings, and so on. ASP.NET Core uses AppSettings.json to store such pieces of information. Consider the following configuration:

The preceding JSON markup consists of two properties or keys, namely AppSettings and Data. The AppSettings property holds a sub-key named Title. The Title sub-key has a string value of "My ASP.NET Core Application". Similarly, the Data key has a DefaultConnection sub-key. The DefaultConnection in turn has a ConnectionString sub-key.

You can read the values stored in AppSettings.json using ConfigurationBuilder and IConfiguration (as discussed later).

4. Application Startup

In ASP.NET, Global.asax acts as the entry point for your application. You can wire various event handlers for events, such as Application_Start and Session_Start, in the Global.asax file. In ASP.NET Core, the application startup takes place quite differently—it happens through a Startup class. Consider one such Startup class, as shown below:

The Startup class shown above begins with a constructor. The constructor loads the AppSettings.json file using ConfigurationBuilder class. The Get() method then is used to read the database connection string stored in the AppSettings.json file.

The ConfigureServices() method adds the services required by the application. For example, here you add MVC and Entity Framework to the services collection.

The Configure() method specifies and configures the services added earlier for application's use. For example, the MVC routing is configured in the code shown above.

Finally, the Main() method acts as an entry point (quite similar to console applications) for the application.

5. Tag Helpers

In ASP.NET MVC 5, you used HTML helpers such as BeginForm(), LabelFor(), and TextBoxFor() to render forms and form fields. You can continue to use HTML helpers in ASP.NET Core, also. But there is a better alternative: Tag Helpers. Tag helpers take the form of standard HTML tags with certain special asp-* attributes added to them. Consider the following markup that renders a form:

Notice the attributes that begin with asp-. They are defined by the tag helpers. For example, the form tag helper uses asp-controller attribute to specify the target controller name and asp-action attribute to specify the target action method name. Similarly, asp-for attributes used with label and input tag helpers bind a label or a textbox to a model property. Tag helpers are more convenient to use than HTML helpers because their syntax closely resembles the HTML markup.

6. View Components

In MVC 5, you used partial views as a means to reuse markup and code. ASP.NET Core introduces View Components—a more powerful and flexible alternative. A view component consists of a class typically inherited from ViewComponent base class and a view file containing the required markup. This programming model is quite similar to the one used by controllers and views. It allows you to separate code and markup from each other—code in the view component class and markup in a view. Once created, you can use a view component on a view by using the @Component.Invoke() method.

Here, MyService is the type to be registered with the DI framework and implements IMyService. The AddSingleton() method registers this type for Singleton mode described above. Once a type is registered with the DI framework, you can inject it in a controller like this:

8. Gulp, Grunt, and Bower Support

Gulp and Grunt are JavaScript task runners. They help you automate commonly needed tasks, such as bundling JavaScript and CSS files, minifying JavaScript and CSS files, and compiling Less and Sass files (and many more). They are installed using npm (Node Package Manager). The ASP.NET Core project created using Visual Studio 2015 allows you to add Grunt and Gulp configuration files and also provides Task Runner Explorer to monitor the tasks. You may find more details about using Grunt and Gulp with ASP.NET Core here.

Bower is a package manager primarily for front-end packages. Front-end packages are the packages that you use in your Web pages, such as JavaScript libraries/frameworks and CSS files. For example, you may install jQuery in your ASP.NET Core project by using Bower. An ASP.NET Core project created using Visual Studio 2015 allows you to add a Bower configuration file. You also can work with the packages using the Manage Bower Packages menu option. Read more details about using Bower with ASP.NET Core.

9. Single Programming Model for MVC and Web API

In MVC 5, controllers inherit from the System.Web.Mvc.Controller base class. And, Web API 2 controllers inherit from System.Web.Http.ApiController. In ASP.NET Core, both of these frameworks are merged into a single framework. Thus, under ASP.NET Core, an MVC controller and Web API controller both inherit from Microsoft.AspNet.Mvc.Controller base class. You then can configure aspects such as HTTP verb mapping and the routing of the controllers as desired.

10. Static Files and the wwwroot Folder

In ASP.NET, there is no fixed location for storing static files such as image files, JavaScript files, and CSS files (developers often used a Content folder to store such files). In ASP.NET Core, all the static files are kept under the wwwroot folder (default). You also can change the name of this folder by using the Project.json file. Consider Figure 2, which shows how the wwwroot folder looks in the Solution Explorer.

Figure 2: The wwwroot folder

After deployment, the wwwroot becomes the Web application's root. All the URLs to static files are resolved with respect to this folder. So, /images/logo.png expects logo.png to be present under the wwwroot/images folder.

Summary

ASP.NET Core 1.0 is a revamped framework geared towards modern cloud based, modular Web applications. Although the new framework preserves the fundamental concepts of MVC 5, ASP.NET developers will find many differences between MVC 5 and ASP.NET Core 1.0. This article enumerated the important new features/concepts that you need to understand to begin your journey with ASP.NET Core 1.0.