首页 > 解决方案 > 这个类中所有问题的原因是什么?

问题描述

当我遇到一个导致很多错误的未知问题时,我正在学习 ASP.NET Core 教程,编写自己的代码。我知道教程中的人正在运行 ASP.NET Core 2.2.4,而我正在运行 ASP.NET Core 2.2.6。

错误在控制器中。这是课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

using Domain;
using Persistance;


namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // With access to the Database Context, this controller can talk to the Database,
        // reading and updating values.
        private readonly DatabaseContext _DbContext;


        // CONSTRUCTOR INJECTION: Injecting the DatabaseContext service into the Values
        // class.
        public ValuesController(DatabaseContext dbContext)
        {
            this._DbContext = dbContext;
        }


        // GET api/values
        // This is made an Async method to push a task into a second thread: the act
        // of calling the database and retrieving the query results. By doing this, you
        // avoid blocking the main thread. 
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Value>>> Get()
        {
            // Retrieve a List of values from the 'Values' table. 
            var listOfValues = await _DbContext.Values.ToListAsync();
            // Return HTTP Status Code 200, along with the retrieved list of values. 
            return Ok(listOfValues);
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Value>> Get(int id)
        {
            // There are several methods to help you get values from databases 
            // asynchronously: FindAsync(), FirstOrDefaultAsync(), SingleAsync(), and 
            // SingleOrDefaultAsync(). FindAsync() is used here because it is the most
            // appropriate - it will search the database by primary key and return the
            // value if found or 'null' if not. 
            var searchValue = await _DbContext.Values.FindAsync(id);
            return Ok(searchValue);
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}

其中大部分是从模板生成的,我开始按照教程进行更改。在我开始使用 Get(int id) 之前,那里没有问题。那是 Visual Studio Code 开始一直给我错误的时候。据我所知,我已经按照教程完成了所有工作。

如果 Get() 和 Get(int id) 方法之间有两个空格,则后一种方法的标题会出现几个红色下划线。具体来说,关键字“ActionResult”、“Value”、“Get”、“int”以及方法标题中的最后一个“>”和“)”都带有下划线。

错误抱怨:

Syntax error, ','(CS1003)  
Syntax error, '>'(CS1003)  
Syntax error,> ','(CS1003)  
Syntax error, ','(CS1003)  
Syntax error, ','(CS1003) 
Tuple must contain at least two elements. [...\Reactivities\API\API.csproj](CS8124)  
Identifier expected [...\Reactivities\API\API.csproj](CS1001)  
) expected [...(CS1026)

如果我将 Get() 和 Get(int id) 方法之间的间隔从两行缩小到一行,则方法标题中的所有红色波浪形都会消失,而只有 Get(int id) 中的左大括号会变成红色波浪形线。

这些是相关的错误:

Syntax error, ','(CS1003)  
Syntax error, '>'(CS1003)  
Identifier expected [...\Reactivities\API\API.csproj](CS1001)  
Syntax error, ','(CS1003)  
Syntax error, ','(CS1003)  
Identifier expected [...\Reactivities\API\API.csproj](CS1001)  
Syntax error, ','(CS1003)  
Tuple must contain at least two elements. [...\Reactivities\API\API.csproj](CS8124)  
Identifier expected [...\Reactivities\API\API.csproj](CS1001)  
) expected [...(CS1026)  

我认为这是某种语法错误,但我不知道它可能是什么。有人想看看这段代码吗?

标签: c#asp.net-core

解决方案


这些类型的错误意味着:

  • 某处存在语法错误
  • 语法错误太糟糕了,编译器甚至不能告诉你它在哪里了。确实太糟糕了,他会产生十几个其他错误,徒劳地试图理解那个输入。

真正的错误将是带有错误报告的第一行。或者在那之前。一个常见的错误是多余/缺少分号。后跟多余的/缺少的引号。但即使仔细检查,我也找不到代码有明显问题。


推荐阅读