-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Apologies if this has been publicly discussed already, but I can't find anything about it and the terms are generic enough to make searching very difficult.
It seems that .NET6 (RC2, with VS2022 RC2) has marked the model passed to an MVC page via an @model directive as nullable, irrespective of whether the type supplied by the view's author is nullable or not.
I'm sure this has some technical correctness about it, but it seems here that this may fall on the wrong side of the trade-offs that surround the use of nullable reference types. (I love compiler warnings, and was putting <nullable>enable</enable> all over stuff from the first minute it was available.)
We now have hundreds and hundreds of warnings across views which have been written years ago. These views would have no useful function whatsoever if someone decided to call them with a null model: an NRE which ended-up in our standard exception handler would be just as good a way of handling the situation as any other.
I couldn't find a way of adding exclamation marks to the @model line itself, and the fact that we haven't marked our model types as nullable is not respected by the compiler.
Am I missing something here, or are we really expected to choose between adding exclamation marks all over "legacy" views or foregoing nullable-enable altogether? Or is there perhaps a new alternative to the @model directive which uses the type supplied without inferring a "?" on the end? Changing one line at the top of the view would be better than mods all over it.
The only example I can find in current templates is in "Error.cshtml", which contains some of this sort of stuff: @if (Model?.ShowRequestId ?? false) - which may well be a good idea in an error handler where you really do want to avoid exceptions. In most other views it seems much better to throw the NRE and know about the catastrophe than to undetectably (from the site-owner's POV) have bits of pages mysteriously go missing.