Aspnetcore: Receive "The returnUrl field is required" validation error when enabling Nullable reference types

Created on 8 Jun 2020  路  11Comments  路  Source: dotnet/aspnetcore

Describe the bug

When enabling nullable reference types I receive a validation error "The returnUrl field is required" when navigating to:
~/Identity/Account/Register
or
~/Identity/Account/Login

To Reproduce

Start from a new aspnetcore identity template with individual account, all views scaffolded. Then enable Nullable reference types in the project,

<Nullable>enable</Nullable>

Navigate to ~/Identity/Account/Register. You should see in the validation summary

"The returnUrl field is required"

Further technical details

  • ASP.NET Core version Microsoft.AspNetCore.App 3.1.4

image

image

area-identity

Most helpful comment

Changing the OnGetAsync parameter

from
public async Task OnGetAsync(string returnUrl = null)

to
public async Task OnGetAsync(string? returnUrl = null)

Solves the error. Maybe good to note in documentation somewhere as it took a bit to find the exact cause. Hopefully helps others...

All 11 comments

Changing the OnGetAsync parameter

from
public async Task OnGetAsync(string returnUrl = null)

to
public async Task OnGetAsync(string? returnUrl = null)

Solves the error. Maybe good to note in documentation somewhere as it took a bit to find the exact cause. Hopefully helps others...

What do we want to do here @blowdart generating different scaffolding code based on nullability options is not something we currently are planning are we?

Oh dear.

I honestly don't know, I'd assume that we want to support nullability, which in turn may need scaffolding changes. @Pilchie @DamianEdwards this is probably an opinion for you to weigh in on.

I'm confused as to how this causes a runtime error? The nullable annotations in the C# compiler shouldn't change the generated code afaik.

Yeah its weird that turning on the feature would cause a different runtime behavior, but wrt to the general question:

From https://devblogs.microsoft.com/dotnet/embracing-nullable-reference-types/

"We strongly encourage authors of libraries (and similar infrastructure, such as code generators) to adopt NRTs during the nullable rollout phase."

Should we be updating the identity ui code + scaffolded output to support nullable reference types in 5.0, basically to make this flow just work (scaffold all + enable nullable)

Ideally yes. @pranavkm has already been looking at adding nullable annotations to a bunch of the ASP.NET Core code in the last few weeks.

MVC infers that all non-nullable types are required - https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-3.1#required-attribute. string returnUrl is non-nullable, which would explain the error message.

If we change the template would it break if you have have nullable types off?

You can enable or disable nullables on individual C# files using pragmas.

#nullable enable

I don't know what the equivalent gesture is for .cshtml files (@NTaylorMullen).

I don't know what the equivalent gesture is for .cshtml files (@NTaylorMullen).

We don't have one to apply to the entire file sadly. Razor files inherit the projects nullability so if you turn it on the for the entire project then Razor files will have it turned on.

Closing in favor of aggregating issue, #25310

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FourLeafClover picture FourLeafClover  路  3Comments

Pixel-Lord picture Pixel-Lord  路  3Comments

ermithun picture ermithun  路  3Comments

UweKeim picture UweKeim  路  3Comments

Kevenvz picture Kevenvz  路  3Comments