5 New MVC features in .NET 7

Web development
Feb 08, 2023
4 Mins Read
5 New MVC features in .NET 7

Many notable features added to ASP .NET Core in .NET 7 included simple APIs like route groups and filters. However, MVC controllers were not discarded just because essential APIs were a primary focus! In this article, We go through 5 new MVC features that .NET 7 brought to the table.

New features are:

1. IParseable / TryParse for primitive binding

2. MVC Controllers can automatically infer

3. Inferring necessary via nullable annotations

  • Inferring body nullability
  • Inferring service optionality

4. IResult is supported in MVC

5. Better error names are provided in answers by customized metadata providers.

Although I refer to them as MVC controllers throughout this piece, the majority of these functionalities also apply to API controllers, Razor Pages, and MVC controllers.

Iparseable / Tryparse for Primitive Binding

Model binding is one of the primary differences between MVC and essential APIs. Since the early days of ASP .NET, MVC has used a “binding provider” technique that is virtually unchanged today.

With this technique, you can link your action method types to form data, headers, query strings, and many other things.

Simple types like decimal and DateTime are bound by default in MVC and any other types with a TypeConverter to string. Anything else is regarded as a complex type, which implies that it either attaches to the request body or binds as “a complex type.” As a complex type, binding refers to binding each type’s constituent property rather than the class itself.

Mvc Controllers can Automatically Infer

The automated inference of services registered in DI, rather than requiring an explicit [FromServices] element, is another essential API innovation that made its way to MVC. 

For instance, using critical APIs, you may insert services into the endpoints themselves:

The [FromServices] annotation is required in .NET 6; otherwise, MVC will attempt to bind the links parameter to the request body.

You may get rid of the [FromServices] attribute in .NET 7.

Inferring Necessary Via Nullable Annotations

The support for nullable reference types improves with each new .NET release. Support for ASP .NET Core was implemented in a few areas in .NET 7:

  • We infer the nullability of the parameter from the nullability of the Body.
  • The nullability of the parameter determines whether a [FromServices] parameter is needed.
Inferring Body Nullability

When binding to a request’s body in .NET 6, where Content-Length == 0, either explicitly using [FromBody] or when this is inferred, the attempt will fail with an error message. The request body must be complete.

Well, with .NET 7, everything gets a lot easier. 

Nullable annotations can be used in place of adding the attribute. EmptyBodyBehavior. A nullable parameter implies Disallow. Allow; a non-nullable type indicates an empty body behavior.

Inferring Service Optionality

It has to do with the [FromService] annotation in this instance. Even though the decorated argument in .NET 6 is designated nullable, calling the API results in an InvalidOperationException.

A [FromServices] parameter set as nullable will now be null in .NET 7 if it isn’t present in DI.

Iresult is Supported in MVC

As you may expect, MVC now has limited support for serializing IResult objects in .NET 7. You never get MVC capabilities like content negotiation and output formatters when you return IResult from an MVC action; you always get JSON instead. 

However, this has the advantage that you may share more helpers and components across your simple APIs and MVC controllers by only returning IResult, as opposed to needing to have parallel processing for both IActionResult and IResult, if you’re only building a JSON API.

Better Error Names are provided in Answers by Customised Metadata Providers

A new ModelMetadataDetailsProvider was added to .NET 7 that, in theory, should allow you to control As I understand the documentation and the initial issue,

with camelCase serving as both the dictionary key and the message’s name.

In contrast, I could do this if I manually specified the MVC JsonOptions to use camelCase for dictionary keys. The dictionary keys are camelCase in this configuration, but the messages still utilize the property names:

Final Words:

I discussed a few of the new MVC features added in .NET 7 in this post. Many features are intended to make working with both MVC and minimal APIs in the same application simpler or to bring parity with minimal APIs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts