BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Minimal APIs, Blazor Improvements, and Tools Updates in ASP.NET Core 6

Minimal APIs, Blazor Improvements, and Tools Updates in ASP.NET Core 6

This item in japanese

Last year, Microsoft released the latest version of the .NET framework, the .NET 6. This version includes many new features and improvements related to the ASP.NET Core framework, increasing the capabilities available for developers and simplifying the development process.

ASP.NET Core project templates now include built-in support for Angular 12, React 17, and Bootstrap 5.1. Starting from .NET 6, all .NET project templates have the null-state analysis feature enabled by default, a set of helpful warnings to identify null references and avoid the System.NullReferenceException.

Hot Reload, released at the beginning of 2021 for C#, Razor, and stylesheet files, now supports all ASP.NET Core projects. Using this feature, developers can change their code while running without the need to recompile or restart the debug session. Hot Reload is available in Visual Studio and Visual Studio Code through the dotnet watch command.

.NET WebAssembly build tools are a set of tools designed explicitly for Blazor WebAssembly, adding support to ahead-of-time (AOT) compilation, relinking, and native dependencies. Users can install these tools using the Visual Studio installer or the dotnet workload install wasm-tools command in a shell window.

With AOT compilation, the application code is compiled into WebAssembly and directly executed by the browser without using a .NET Intermediate Language (IL) interpreter. This process increases applications' performance at the expense of the app size, which is more significant. The AOT feature can be enabled in the project file with the following lines:

<PropertyGroup>
  <RunAOTCompilation>true</RunAOTCompilation>
</PropertyGroup>

Runtime relinking allows trimming unused code from the dotnet.wasm, the .NET WebAssembly runtime, decreasing its size and improving the download speed at the first request.

Minimal APIs help developers to create straightforward ASP.NET APIs operating a minimum set of classes, which is useful for a microservices architecture. Routes are directly defined in the Program.cs class, avoiding the implementation of a specific controller for each route and reducing the code size. Here is an example of how to define routes:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "This is a GET");
app.MapPost("/", () => "This is a POST");
app.MapPut("/", () => "This is a PUT");
app.MapDelete("/", () => "This is a DELETE");

app.Run();

Unfortunately, Minimal APIs don't support all the facilities of an ASP.NET MVC controller, such as filters.

Async streaming is now available in ASP.NET controller actions and data conversions based on the JSON formatter. Thanks to the IAsyncEnumerable, data is not buffered anymore, significantly reducing memory usage. This option can be used with Entity Framework Core for enumerating data asynchronously straight from the database (lazy loading must be disabled). IAsyncDisposable has been updated to support resource disposal in controllers, Razor Pages, and view components. The interface must be implemented through the DisposeAsync() method, especially when working with asynchronous streams.

ASP.NET Core 6 introduces the concept of CSS isolation, allowing the definition of CSS styles for a specific page or view. The CSS files must be defined with the same name of the page or view using the .css extension. For example, styles that relate only to the Index.cshtml page must be defined in an Index.cshtml.css file. During the build process, these files are bundled into one named as "{APP ASSEMBLY}.styles.css", which is referenced in the _Layout.cshtml file.

Another feature similar to the CSS isolation is related to JavaScript modules. Developers can define JavaScript code in an Index.cshtml.js file, which is publicly accessible using the pattern "{PATH}/{PAGE, VIEW, OR COMPONENT}.{EXTENSION}" (pages, views, and components) or the pattern "_content/{PACKAGE ID}/{PATH}/{PAGE, VIEW, OR COMPONENT}.{EXTENSION}" (Razor class library).

Lots of improvements for this ASP.NET Core release are related to Blazor. It is now possible to prerender and integrate Razor Pages and MVC apps in a Blazor Server app. Also, the Razor components can be dynamically rendered using JavaScript or the DynamicComponent attribute (to render by type). Also, the content of the HTML <head> element can be modified at run time using the HeadContent component.
The same components can have required parameters by using the EditorRequired attribute as in the following code snippet:

[Parameter]
[EditorRequired]
public string? FirstName { get; set; }

In addition, the attribute SupplyParameterFromQuery allows to specify a parameter from the query string:

[Parameter]
[SupplyParameterFromQuery(Name = "{QUERY PARAMETER NAME}")]
public string? {COMPONENT PARAMETER NAME} { get; set; }

Networking was also greatly improved in this new release. Thanks to the improvements to System.IO.Pipelines and to SslStream, WebSocket connections reduce the memory size and the per-connection overhead by about 30% (according to Microsoft).

The introduction of HTTP Logging middleware helps identify problems in applications by logging requests and responses information (i.e., headers, body, common properties). Different options are available to filter the set of information exposed. W3CLogger is another middleware that helps log the same information as HTTP Logging, but the output uses the W3C standard format. Both middlewares can have an impact on application performances or expose sensitive information.

Finally, ASP.NET Core 6 now supports HTTP/3 as a preview feature, and it is not enabled by default; developers must enable it in the Kestrel configuration. The main advantage is that the latest HTTP version uses QUIC as a new transport technology, which decreases the response of the first request and has native multiplexing support.

The current version of ASP.NET Core is 6.0.1. It also includes security fixes, such as the CVE-2021-43877 (a vulnerability that allows the elevation of privilege when .NET 6 applications are hosted within IIS) and improvements regarding exception messages for application startup code. In the ASP.NET MVC framework, the development team fixed exceptions in the TagHelperMatchingConvention attribute and typos in the protobuf and Web API templates.  

Microsoft's official documentation contains all the features of ASP.NET Core 6. Its latest release version can be downloaded here. An existing application using previous versions of ASP.NET Core can be upgraded by following these steps.

 

About the Author

Rate this Article

Adoption
Style

BT