-3

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.

Mixture
  • 43
  • 1
  • 2
  • 7
  • 1
    Is your project targeting C# 8? Are you using Visual Studio 2019? – Jonathon Chase Jan 24 '20 at 19:09
  • Please don't post pictures of your code, actually paste the code into your question. See [how to ask a question](https://stackoverflow.com/help/how-to-ask) – gabriel.hayes Jan 24 '20 at 19:10
  • @JonathonChase yes im using VS 2019 – Mixture Jan 24 '20 at 19:11
  • 1
    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 Jan 24 '20 at 19:11
  • Done @Christopher hmm.. the project was building normally suddenly this error appeared.. what do you think should be done? – Mixture Jan 24 '20 at 19:18
  • @Mixture As I read it f rom my source below, C# 8.0 in VS 2019 is only avalible if you Target .NET Core 3 or Later. All others seem to be limited to C# 7.3. However this information is only second hand for me. – Christopher Jan 24 '20 at 19:22
  • @Christopher: No, that's just the default. You can specify `8.0` explicitly in the project file. – Jon Skeet Jan 24 '20 at 20:23

3 Answers3

1

??= 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/

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
Christopher
  • 9,634
  • 2
  • 17
  • 31
  • 1
    I believe it's the 'Null Coalescing Assignment' operator, and it's a C# 8 feature. – Jonathon Chase Jan 24 '20 at 19:26
  • @JonathonChase Then I think you are right. I had to look all that up. Also with VS 2019 there seems to be an issue wich C# version you are using, depending on the target Framework. So language version is the most likely cause. – Christopher Jan 24 '20 at 19:27
1

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");
Jonathon Chase
  • 9,396
  • 21
  • 39
0

Install the Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet package in the Web Application project so you can use newer syntax.

Keith Banner
  • 602
  • 1
  • 10
  • 15