【问题标题】:How to solve Error CS1525 Invalid expression term '='?如何解决错误 CS1525 Invalid expression term '='?
【发布时间】:2020-05-11 03:05:52
【问题描述】:

这是注册类中post方法的代码:

    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();
    }

错误是“无效的表达式术语'='”,它指向行 return ??=Url.Content("~/Account/feeds"); 知道这个类是一个脚手架项目,我该如何解决这个错误??

谢谢。

【问题讨论】:

  • 您的项目是否面向 C# 8?你在使用 Visual Studio 2019 吗?
  • 请不要张贴您的代码图片,实际上是将代码粘贴到您的问题中。见how to ask a question
  • @JonathonChase 是的,我正在使用 VS 2019
  • 请将代码发布为文本,而不是图像。也最好是一个最小的、完整的、可验证的例子。不过我同意乔纳森的观点,看起来您尝试使用仅在更高版本的 C# 版本中引入的语法糖。
  • 完成@Christopher 嗯.. 项目正常构建突然出现这个错误.. 你认为应该怎么做?

标签: c# asp.net asp.net-core compiler-errors scaffold


【解决方案1】:

Null Coalescing Assignment 运算符 (??=) 是 C# 8 中引入的一项功能。看来您的项目可能没有正确定位该语言版本,或者可能无法定位它。

您可以相当容易地获得与此运算符所做的基本等效的功能,尽管它有点冗长。

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

你也可以只检查值是否为空,然后分配给它。

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

【讨论】:

    【解决方案2】:

    ??= 是Null Coalescing Operator。那是语法糖。它是在 C# 7.3 中添加的,并在 8.0 中进行了改进。

    请注意,C# 版本完全是编译器。它与目标框架无关。但是,后端框架确实很重要,从某种意义上说,真正的命令行编译器 VS 正在远程处理可能还不支持新的 C# 方言:

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

    【讨论】:

    • 我相信它是“Null Coalescing Assignment”运算符,它是 C# 8 的功能。
    • @JonathonChase 那么我认为你是对的。我不得不查查这一切。同样对于 VS 2019,您使用的 C# 版本似乎存在问题,具体取决于目标框架。所以语言版本是最可能的原因。
    猜你喜欢
    • 1970-01-01
    • 2019-08-16
    • 2019-11-25
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2019-07-01
    相关资源
    最近更新 更多