Routing is a fundamental part of building web applications with ASP.NET Core. In .NET 8 Preview 1, the route tooling has received several new features that make it easier to work with and learn. These features include:
Routing is a powerful and high-performance technology used by almost all ASP.NET Core apps, and route constraints are a commonly used feature of routing that helps match requests to the right endpoint. In .NET 8, several performance improvements have been made to constraints to maximize runtime performance and reduce startup time and memory use:
// Endpoint only matches requests with a URL slug. For example: article/my-article-nameapp.MapGet("article/{name:regex(^[a-z0-9]+(?:-[a-z0-9]+)*$)}", (stringname) => { /* ... */ });
In this example, a regex constraint is used to ensure that the “name” parameter matches a specific pattern. In .NET 8, several performance improvements have been made to constraints:
Developers know that high-quality code is easier to debug, test, and maintain. In .NET 8, Microsoft has introduced a suite of new analyzers that provide warnings and recommendations to improve the quality of ASP.NET Core APIs.
One of the new analyzers recommends setting or appending to a
HeaderDictionary
instead of using the
IHeaderDictionary.Add
API, which can throw an exception if duplicate keys are added. Instead, developers can leverage the indexer or the
IHeaderDictionary.Append
API to set, override, or append to a header. Here’s an example of using
IHeaderDictionary.Append
:
varcontext = newDefaultHttpContext();context.Request.Headers.Append("Accept", "text/html"); // No warning issued
Another analyzer warns if a parameter type passed to a route handler does not implement the correct interfaces. For example, the
Customer
type in the code below will trigger a warning because it does not implement a
BindAsync
method with the correct signature:
This warning helps developers ensure that their APIs are robust and that the appropriate interfaces are implemented for proper operation.
Finally, the RequestDelegate analyzer detects if a RequestDelegate is implemented incorrectly. This analyzer ensures that the response of the RequestDelegate is not discarded at runtime and recommends the use of c.Response.WriteAsync or minimal APIs instead. Here’s an example of using c.Response.WriteAsync:
These new analyzers are a valuable addition to the ASP.NET Core development experience, helping developers create high-quality code that is easier to debug, test, and maintain. Microsoft would like to thank the community members who contributed to these analyzers and helped improve the ASP.NET Core development experience.
Blazor WebAssembly Debugging in Firefox
Debugging Blazor WebAssembly apps is now possible using Firefox, thanks to the latest updates in .NET 8 Preview 1. Debugging Blazor WebAssembly apps requires configuring the browser for remote debugging and then connecting to the browser using the browser developer tools through the .NET WebAssembly debugging proxy. At this time, debugging Firefox from Visual Studio is not supported.
To debug a Blazor WebAssembly app in Firefox during development, follow these steps:
Follow the instructions in the dev console output to configure Firefox for Blazor WebAssembly debugging:
Open about:config in Firefox
Enable devtools.debugger.remote-enabled
Enable devtools.chrome.enabled
Disable devtools.debugger.prompt-connection
Close all Firefox instances and reopen Firefox with remote debugging enabled by running firefox --start-debugger-server 6000 -new-tab about:debugging.
In some environments, firewalls and anti-virus tools block the download or use of .dll files, which prevents the use of web apps based on .NET assemblies, like Blazor WebAssembly apps. To address this issue, the wasm-experimental workload now supports a new .webcil file format that can be used to package .NET assemblies for browser-based web apps. This is a valuable addition to the .NET ecosystem as it helps developers deploy apps more easily in environments where .dll files are blocked.
To try out the new Webcil format with a WebAssembly Browser App, follow these steps:
The Webcil format is currently only available for the experimental WebAssembly Browser Apps, but Microsoft plans to enable it for Blazor WebAssembly apps in a future update. If you encounter issues with using .webcil files in your environment, please let Microsoft know by creating an issue on GitHub.
Specify initial URL for BlazorWebView to load
The new StartPath property on BlazorWebView allows you to set the path to initially navigate to. This is useful when you want to take the user straight to a specific page once the BlazorWebView is loaded.
New option to keep the SPA development server running
When building a single-page app (SPA) with ASP.NET Core, both the SPA development server and the backend ASP.NET Core need to execute during development. The SPA development server is configured to proxy API requests to the ASP.NET Core backend.
When the ASP.NET Core process is terminated, the SPA development server is by default terminated as well. Restarting the SPA development server can be time consuming and cumbersome.
The new KeepRunning option on SpaDevelopmentServerOptions enables leaving the SPA development server running even if the ASP.NET Core process is terminated.
Named pipes is a popular technology for building inter-process communication (IPC) between Windows apps. You can now build an IPC server using .NET, Kestrel, and named pipes.
HTTP/3 is a new internet technology that offers several advantages over older HTTP protocols, including faster connection setup, no head-of-line blocking, and better transitions between networks. In .NET 7, support for HTTP/3 was added to ASP.NET Core and Kestrel, but in .NET 8, it is enabled by default alongside HTTP/1.1 and HTTP/2.
In the above example, we are using the UseKestrel method to configure Kestrel as the web server and Listen to specify the IP address and port to listen on. The HttpProtocols.Http3 option is used to enable HTTP/3.
Note that this example assumes you have already added the necessary packages for HTTP/3 support, such as the Microsoft.AspNetCore.Server.Kestrel.Https package.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.