Skip to content

.NET 7: Microsoft Reveals New ASP.NET Core Features

25 de August de 2022
asp.net core preview 2 in .net 7

Microsoft has revealed more new features that will be included in its next version of ASP.NET Core for .NET 7. This new preview includes API enhancements, cookie consent customization, support for shadow copying and more. Let’s take a look at what’s new in ASP.NET Core in .NET 7!

Infer service-sourced API controller action parameters

Previously it was necessary to apply the [FromServices] attribute to the parameters. Now thanks to this update, parameter binding for API controller actions has the ability to bind them via dependency injections.

Let’s look at the Microsoft example:

But this is not mandatory. Apart from being able to link the parameters by injecting them, we can also simply disable it in case we do not want to use it. In order to disable this we simply need to use DisableImplicitFromServicesParameters.

Let’s look again at the example provided by Microsoft:

If you want to know more about this feature, I recommend you, as always, to consult the original source: Infer API controller action parameters that come from services

Dependency injection for SignalR hub methods

If you remember ASP.NET Core Preview 1 in .NET 7, you will recall that SignalR enhancements were added. In the last Preview a new client source generator for SignalR was added.

Now, in this preview, using Dependency Injection (DI) we have the possibility to inject services with the SignalR hub methods (thanks Microsoft for adding this support):

In addition, we can explicitly mark only one parameter. This is useful to be able to link only that parameter from the configured services. To do this, simply configure the [FromServices] attribute:

And of course, we also have the possibility to disable this dependency injection. To do this, again following Microsoft’s example, we must configure DisableImplicitFromServicesParameters:

If you want to know more about this feature, I recommend you, as always, to consult the original source: Dependency injection for SignalR hub methods

Minimal API endpoint summaries and descriptions

As a new feature in this Preview, it is now possible to set summaries and descriptions for route handlers. This feature is because Microsoft has added support for annotations, descriptions and operation summaries in the minimal APIs. This is mainly used for generating OpenAPI specifications.

Let’s see how Microsoft applies it in its example with extension methods:

Another thing we can do is to use an attribute on the route handler delegate to set a summary or description. According to the example:

If you want to know more about this feature, I recommend you, as always, to consult the original source: Provide endpoint descriptions and summaries for minimal APIs

Bind HTTPS header and query string values to arrays

This is the next feature presented in this second preview by Microsoft. What this feature allows us is the ability to bind query strings and HTTPS header values to StringValues or any primitive type array.

For example if we want to bind to a string array:

To bind query string values to a primitive type array:

And to bind to StringValues :

You can check these examples here: Binding arrays and StringValues from headers and query strings in minimal APIs

Cookie consent value customization

This feature was contributed by David De Smet. This feature allows to use the property CookiePolicyOptions.ConsentCookieValue to add a custom value for tracking (only if the user or visitor accepts the cookie policy).

In Dotnet’s Github they provide this example:

There is little more information about this feature. If you want to know more about it I recommend you to check this feature in Github

Shadow copying for IIS request

Shadow copying allows you to update the different assemblies of an application while it is running. When an ASP.NET Core application is running on Windows, these binaries are blocked to prevent any modification. Obviously it is possible to stop the app by deploying a copy of the file offline, but in most cases this is not ideal. For this, this shadow copying feature can make a copy of the assemblies so that it can be updated.

In order to enable this feature, according to Microsoft, you simply need to modify the ANCM handler settings in the web.config file:

Microsoft also adds:

“We’re investigating making shadow copying in IIS a feature of ASP.NET Core in .NET 7, and we’re seeking additional feedback on whether the feature satisfies user requirements.”

If you want to know more about this feature, I recommend you, as always, to consult the original source: Request for feedback on shadow copying for IIS

At this time, we continue to keep our fingers crossed that Microsoft will release some fresh details soon. What new things does Microsoft have in mind for us over the next several weeks or months? What will take us by surprise? Microsoft is the only one who knows.

Settings