I have tried to copy/rewrite that code for learning purposed and realized after far to much hours:
sneakly hidden before all the using statements: #nullable disable
It was driving me insane, that in my code (opposed to example) that the ModelState
was always invalid with the parameter returnUrl
present in the method signature:
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
if (ModelState.IsValid)
{
//...
}
}
WHY ?
I was under the impression that, enabling nullable types, WILL NEVER break backwards-compatibility as in simply enabling/disabling it would never change how the same code works. But appareantly, the framework does some black magic and reports a different value for ModelState
. This is extremely unexpected (for me). That means that actually a bool holds different value, simply by enabling/disabling nullability!
Sidenote: Since I try to work through the example, I really don't need the paramter for my implementation, and I assume, maybe, when using parameters (I assume from queries) like this, the implementaion could be made more stable, that either nulls are never generated or nulls are accaptable for the action handler ??
But I'm scared now, do I have to pay special attention to the code in various places, where the RazorPages/ASP.NET Core/Blazor framework behaves differently for nullability enabled/disabled?
What are best practices? Simply disable nullable for RazorPages like in example?
Critique about 'nullable' in Razor Pages:
https://andrewlock.net/fighting-with-nullable-reference-types-in-razor-pages/