1
votes

This is the code of the post method in the register class :

    public async Task<IActionResult> OnPostAsync(string returnUrl = null)
    {
       returnUrl ??= Url.Content("~/Account/feeds");
        ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
        if (ModelState.IsValid)
        {

            var user = new ApplicationUser { 
                UserName = Input.username,
                Email = Input.Email,
                DateOfBirth = Input.DOB,
                Gender = Input.Gender
            };
            //this one too

            //notice ma3aml dbcontext.add(user) batteekh w 3amal save..
            var result = await _userManager.CreateAsync(user, Input.Password);
            if (result.Succeeded)
            {
                _logger.LogInformation("User created a new account with password.");

                /*var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                var callbackUrl = Url.Page(
                    "/Account/ConfirmEmail",
                    pageHandler: null,
                    values: new { area = "Identity", userId = user.Id, code = code },
                    protocol: Request.Scheme);

                await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                    $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");*/

                if (_userManager.Options.SignIn.RequireConfirmedAccount)
                {
                    return RedirectToPage("RegisterConfirmation", new { email = Input.Email });
                }
                else
                {
                    await _signInManager.SignInAsync(user, isPersistent: false);
                    return LocalRedirect(returnUrl);
                }
            }
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
        }

         //If we got this far, something failed, redisplay form
        return Page();
    }

The error is "Invalid expression term '=' " and it points to the line of return ??=Url.Content("~/Account/feeds"); How can I solve this error knowing that this class is a scaffolded item ??

Thanks.

2
Is your project targeting C# 8? Are you using Visual Studio 2019? - Jonathon Chase
Please don't post pictures of your code, actually paste the code into your question. See how to ask a question - gabriel.hayes
@JonathonChase yes im using VS 2019 - Mixture
Please post code as text, not images. Also preferebly a minimal, complete, verifyable example. However I agree with Jonathan, it looks like you try to use syntax sugar that was only introduced with a later C# version. - Christopher
Done @Christopher hmm.. the project was building normally suddenly this error appeared.. what do you think should be done? - Mixture

2 Answers

1
votes

The Null Coalescing Assignment operator (??=) is a feature introduced in C# 8. It appears that your project may not be correctly targeting this version of the language, or perhaps be unable to target it.

You can get what's essentially equivalent functionality to what this operator does fairly easily, though it's a bit more verbose.

returnUrl = returnUrl ?? Url.Content("~/Account/feeds");

You could also just check if the value is null, and then assign to it.

if(returnUrl == null)
    returnUrl = Url.Content("~/Account/feeds");
1
votes

??= is the Null Coalescing Operator. That one is syntax sugar. It was added with C# 7.3 and improved with 8.0.

Note that the C# Version is entirely a compiler thing. It has nothing to do with the Target Framework. However, the Backend Framework does matter, in the sense the real command line compilers VS is remoting might not yet support a new Dialect of C#:

https://www.c-sharpcorner.com/article/which-version-of-c-sharp-am-i-using-in-visual-studio-2019/