Electronics and eHealth Innovations Transform Healthcare
25 Sep 2023
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.
1. IParseable / TryParse for primitive binding
2. MVC Controllers can automatically infer
3. Inferring necessary via nullable annotations
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.
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.
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.
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:
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.
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.
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.
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:
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.