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

.NET 7: Microsoft Reveals New ASP.NET Core Features
August 25, 2022
6 minutes read

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 .

Let’s look at the Microsoft example:

Loading code snippet...

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:

Loading code snippet...

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  (thanks Microsoft for adding this support):

Loading code snippet...

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:

Loading code snippet...

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:

Loading code snippet...

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 . 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:

Loading code snippet...

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:

Loading code snippet...

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:

Loading code snippet...

To bind query string values to a primitive type array:

Loading code snippet...

And to bind to StringValues :

Loading code snippet...

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

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:

Loading code snippet...

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:

Loading code snippet...

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.

You May Also Like

Optional Parameters in C#: What You Need to Know

Optional Parameters in C#: What You Need to Know

Programming in C# often requires flexibility and simplicity ...

What is Lock Keyword in C#? Main Usages

What is Lock Keyword in C#? Main Usages

IndexUnderstanding the C# Lock Keyword Can’t tell a lock fro...

Enumerate in C#: Detailed Explanation

Enumerate in C#: Detailed Explanation

Desperate to decode the mystery that is enumeration in C#? I...

Creating a JSON Class in C#: Detailed Guide

Creating a JSON Class in C#: Detailed Guide

In the world of application development, manipulation of dat...

Static Class in C#: How to Use It?

Static Class in C#: How to Use It?

Hello there, future C# aficionado! It’s time to roll down th...

DateTime Formatting in C#: Dev Guide

DateTime Formatting in C#: Dev Guide

Have you ever gotten frustrated handling dates and times in ...

Leave a reply

Loading comment form...