Recently, our developer advocates authored a whitepaper that clearly lays out the state of .NET as we enter 2018. In this post, we'll dive into an excerpt about ASP.NET Core.
I’ve been in this industry a long time – long enough to remember when .NET was first introduced – and I must say, it is an exciting time to be a developer in this space. Changes are being made to the offerings, additions are being made to the platform, and Microsoft is making different operating systems and competing devices first-class citizens in its own ecosystem.
While change in this case is good, it is a lot to keep up with. My colleagues, John Bristowe, Ed Charbeneau, and Sam Basu, have co-authored a whitepaper that summarizes the state of our industry. In “The State of .NET in 2018: How the New .NET Standard is Making You a Better Developer,” they give an expert’s overview of the new .NET, the what and the why behind .NET Standard and XAML Standard, the benefits of ASP.NET Core; the new mobile landscape and advancements in Xamarin; a deep dive into UWP; and more.
In this post, let’s zoom into ASP.NET Core 2.0 and break down some of the key findings from our developer advocates’ whitepaper.
ASP.NET Core 2.0 is fresh off the press, with full support for .NET Core 2.0 and lots of tooling enhancements. ASP.NET Core 2.X provides .NET developers all of the tooling and framework features needed to build modern rich web applications. Here are some things to get excited about with ASP.NET Core 2.0:
Developers building web apps with ASP.NET Core 2.0 have a plethora of rich tooling to choose from - come as you are and choose your development tools. Here are your choices:
The good news for developers is .NET tooling is consistent - no matter what be your development tooling. What CLI can do over commands is exactly what Visual Studio does visually. Take a look at some of the app templates supported by 'dotnet new' CLI tooling - you'll see corresponding similar templates on doing File | New in Visual Studio. Some of the app templates are what you expect from ASP.NET - some are unexpected; we'll break things down.
The ASP.NET team has created one of the most streamlined ASP.NET application architectures yet. With ASP.NET Core 2.0, application dependencies are bundled into a single metapackage reference - Microsoft.AspNetCore.All
. Each dependency in the metapackage can be referenced individually, however using the bundled approach offers additional benefits. All the features of ASP.NET Core 2.x and Entity Framework Core 2.x are included in the bundled Microsoft.AspNetCore.All
package. In addition, applications using the Microsoft.AspNetCore.All
metapackage automatically take advantage of the .NET Core Runtime Store. The Runtime Store contains all the runtime assets needed for the application, thus reducing overhead during build/deployments.
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
</ItemGroup>
One of the nicest things about .NET Core 2.0 and ASP.NET Core 2.0 for developers is portability - the framework and tooling is consistent across all platforms. Developers' choice of development OS does not matter - Windows and Mac have an identical experience. Gone is the ASP.NET specific Project.json
file for dependencies and configuration - the familiar .CSProj
file is back, as shown below for a boilerplate ASP.NET Core 2.0 project. Whether the ASP.NET project is scaffolded from Visual Studio File | New Project or through the dotnet new CLI tools, the resulting project has the same exact .CSProj
file - it simply points to the target runtime framework and pulls in the .NET dependencies.
This also means that developers can collaborate more easily and have ASP.NET projects be portable across machines. It is perfectly conceivable to have multiple developers work on the same ASP.NET project, but using different IDEs - one on Visual Studio on Windows, one on Visual Studio for Mac, and yet others on simple text editors. The .SLN
and .CSProj
files are perfectly portable and the code writing experience is the same everywhere.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
</Project>
ASP.NET Core 2.0 web apps support fast streamlined bootstrapping - this is especially true for setup and initialization routines. The granular details of creating the webhost for the app have been abstracted into the default CreateDefaultBuilder
method. Developers retain the flexibility to use the default or create a custom webhost from scratch as needed.
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
Before ASP.NET Core 2.0, Razor view files were published as .CSHTML files, which were compiled at runtime - this just-in-time compilation came at the expense of a performance hit. In older applications, developers could choose to manually precompile Razor views to reduce the published bundle and increase startup time. Now with ASP.NET Core 2.0 project templates, precompilation is enabled by default - this boosting app startup performance.
There is a new programming model for simple web pages in ASP.NET Core - called Razor Pages. Sometimes, you just want to display some content on a page without going through much ceremony, and maybe do a page postback to server - this is exactly what Razor Pages are meant for. Here are few more pointers about Razor Pages to clear the air of mystery:
Here is what ASP.NET Core 2.0 project templates put out for a Razor Pages project - notice the simplicity of the Pages directory, as compared to a full MVC project:
Let's look at a simple Razor Page view - a CSHTML file with a @page
directive on top:
@page
@model AboutModel
@{
ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@Model.CodeBehindModelMessage</h3>
<p>Use this area to provide additional information.</p>
Notice how the view points to a Model and pulls data out of the model. Turns out, the model is defined in a code-behind file - with the same name as the view with .CS at end, simply by convention:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace AspNetCore2Razor.Pages
{
public class AboutModel : PageModel
{
public string CodeBehindModelMessage { get; set; }
public void OnGet()
{
CodeBehindModelMessage = "This is from the code-behind.";
}
}
}
Notice how the model implements a PageModel - a base class that binds together many of the Razor Pages features. The page models can define methods that correspond to HTTP verbs - like Get
, Put
, Delete
and Post
- for supporting CRUD operations when the Razor Page posts back to the server. You can see how this simple model helps support putting content out on Razor Pages and adding dynamic features through simple server roundtrips.
Now, here's a little variation to defining a PageModel for a Razor Page - this time in the View itself:
@page
@model ContactModel
@using Microsoft.AspNetCore.Mvc.RazorPages
@functions {
public class ContactModel : PageModel
{
public string InPageModelMessage { get; set; }
public void OnGet()
{
InPageModelMessage = "This is from the View.";
}
}
}
<h3>@Model.InPageModelMessage</h3>
<address>
One Microsoft Way<br />
Redmond, WA 98052-6399<br />
<abbr title="Phone">P:</abbr>
425.555.0100
</address>
<address>
<strong>Support:</strong> <a href="mailto:support@example.com">support@example.com</a><br />
<strong>Marketing:</strong> <a href="mailto:marketing@example.com">marketing@example.com</a>
</address>
Notice how the PageModel and server-side C# code can be written entirely within a Razor Page view - skipping the code-behind file altogether. While doable, this is not suggested for any complicated pages with substantial code in the PageModel - separation of concerns is always nice.
Traditional ASP.NET web development mostly relies on server-side rendering with Razor and other view engines. However, browsers have come a long way and JavaScript is ubiquitous - one may argue that client-side web development is where things are headed. ASP.NET Core 2.0 is a modern web framework and in no way attempts to hold developers back from building their web apps however they see fit.
Developers have always been able to bring in JavaScript, CSS and other front-end web development artifacts into the ASP.NET stack. However modern full-featured JavaScript frameworks do a lot out of the box and it doesn't make much sense for developers to reinvent the wheel. There are several popular Single Page Application (SPA) type JavaScript frameworks that aid in building fully front-end web apps with navigation, state management, bundling and the works. Turns out, ASP.NET Core 2.0 works very nicely with these SPA frameworks.
Arguably, two of the most popular JavaScript SPA frameworks are Angular and React. And ASP.NET Core 2.0 offers built-in templates - both with dotnet CLI or the Visual Studio File | New Project route. If going the Angular route, below is a sample project as scaffolded by the ASP.NET Core 2.0 Angular template:
Developers should notice a few differences right away, compared to traditional ASP.NET MVC projects:
Below is a sample app component in TypeScript:
import { Component } from '@angular/core';
@Component({
selector: 'app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent { }
Once the ASP.NET Core 2.0 Angular template is done scaffolding, developers have a familiar dotnet run or F5 experience to run the app - notice the app template being different from traditional ASP.NET apps. The experience when using the ASP.NET Core 2.0 React template is identical.
The Angular/React templates in ASP.NET Core 2.0 are meant to get .NET developers to a good starting point with SPA projects. They also include abstractions to fast-track newcomers. Like any other project templates, these new SPA templates aren't required, but rather helpful. Development teams can always build two separate projects - a fully front-end SPA web app with JavaScript frameworks and an ASP.NET WebAPI project serving up data from the backend. ASP.NET is quite happy in this role and will support APIs for a variety of client apps. But most modern SPA/JavaScript web apps need a lot of orchestration - configuring services, bundlers and managing dependencies. This wild-west approach may be intimidating for many .NET developers and this is where the ASP.NET Core 2.0 Angular/React templates come in - they have pre-configured front-end tooling and hook up a bunch of things for developers behind the scenes.
Here's a handful of benefits that come with the SPA templates in ASP.NET Core 2.0:
For more of this .NET goodness, be sure to download the “The State of .NET in 2018: How the New .NET Standard is Making You a Better Developer” whitepaper.
Modern ASP.NET comes in variety of flavors - developers can mix/match server-side C# with client-side JavaScript. Regardless of how you build your web app, one thing remains constant - web apps need sleek UI to serve their purpose and developers should not reinvent the wheel. The Telerik UI suites (and DevCraft bundles for easily getting multiple suites) offer various ways to light up your web apps, no matter how you build them:
Sara Faatz leads the Digital Experience Technology Community Relations team at Progress. She has spent the majority of her career building community, producing events, creating marketing programs and more. When she's not working, she likes diving with sharks, running and watching hockey.