添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Index

Toggle

Route Tooling

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-name
app.MapGet("article/{name:regex(^[a-z0-9]+(?:-[a-z0-9]+)*$)}", (string name) => { /* ... */ });

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 :

var context = new DefaultHttpContext();
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:

var app = WebApplication.Create();
app.MapGet("/customers/{customer}", (Customer customer) => { /* ... */ }); // ASP0021 warning
public class Customer
    public async static Task<Customer> BindAsync(HttpContext context) => new Customer();
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:

var app = WebApplication.Create();
Task HelloWorld(HttpContext c) => c.Response.WriteAsync("Hello World!"); // No warning issued 
app.MapGet("/", HelloWorld); 

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.

    <BlazorWebView StartPath="/counter" HostPage="index.html" />

    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.

    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddSpaStaticFiles(configuration =>
        configuration.RootPath = "ClientApp/build";
    builder.Services.AddControllers();
    builder.Services.AddSpa(options =>
        options.SourcePath = "ClientApp";
        options.UseReactDevelopmentServer(npmScript: "start");
        options.Options.SourcePath = "ClientApp";
        options.Options.StartupTimeout = TimeSpan.FromSeconds(120);
        options.Options.KeepRunning = true; // Enables the new option
    

    Support for named pipes in Kestrel

    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.

    var builder = WebApplication.CreateBuilder(args);
    builder.WebHost.ConfigureKestrel(serverOptions =>
        serverOptions.ListenNamedPipe("MyPipeName");
    

    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.

    Some key points about HTTP/3 in .NET 8:

    options.Listen(IPAddress.Any, 5001, listenOptions => listenOptions.Protocols = HttpProtocols.Http3; .UseStartup<Startup>();

    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.

    Overview of .NET 9 Preview 4 Ready to dive deep into the latest from the .NET ecosystem? In this section, we'll explore what...

    read more

    Introduction to Hexagonal Architecture in .NET In this comprehensive guide, we'll walk you through everything you need to know...

    read more

    Learn Parallel Programming with C# and .NET can drastically improve your application's performance. With C# and .NET, you have...

    read more
    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.