【问题标题】:Why is `dotnet new webapi` creating a different project for me than other people?为什么 `dotnet new webapi` 为我创建的项目与其他人不同?
【发布时间】:2020-06-29 00:54:50
【问题描述】:

您好,我正在按照教程中的说明进行操作。使用dotnet new webapi,我得到了一个与课程所示不同的项目,并且来自我测试过的另一个人的计算机。我们得到的文件基本相同,但我有一个额外的WeatherForcast.cs,而不是ValuesController.cs,我得到的是WeatherForcastController.cs,其中包含完全不同的代码,我将在底部发布。

Program.csStartup.cs 也有一些较小的差异

造成这种差异的原因是什么?如何从示例中按照我尝试的方式生成文件?

回答:答案是它默认为我安装的 .NET SDK 3.1,但本教程使用 .NET SDK 2.2。运行命令 dotnet new webapi --framework netcoreapp2.2 会得到我正在寻找的这个 webapi 的版本。


我的控制器WeatherForcastController.cs

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

namespace Testing.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

示例及其他电脑控制器ValuesController.cs

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

namespace CretaceousPark.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return "value";
        }

        // 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)
        {
        }
    }
}

【问题讨论】:

  • 检查您使用的 .net 核心版本。版本可能与教程中的版本和您安装的版本不同。

标签: c# asp.net asp.net-core asp.net-web-api asp.net-core-webapi


【解决方案1】:

它基于您安装的 .NET Core SDK 的版本。如果您想完全按照教程进行操作,请安装该特定版本。并摆脱任何较新的版本。学完教程后,安装最新的。

【讨论】:

  • 这很有趣,谢谢你的回答。我的机器上同时安装了 2.2 和 3.1,有什么方法可以在命令中添加要使用的版本,例如 dotnet new webapi -v 2.2.0,就像添加软件包时一样?
  • 我找到了,2.2 出于某种原因无法运行,但运行 dotnet new webapi --framework netcoreapp2.1 将安装本教程中的版本。感谢您的帮助
【解决方案2】:

因为您的计算机上有 .net core 3.x SDK 版本,而创建 Values Controller 的其他计算机有 2.x SDK。

【讨论】:

    【解决方案3】:

    正如其他答案中已经指出的那样,这是由于所使用的 SDK 不同所致。 即使您安装了相同的 SDK,但也安装了较新的 SDK,默认情况下将使用较新的 SDK。 但是,您可以通过在运行命令的目录中添加 global.json 文件来固定 dotnet new 命令使用的 SDK 版本,其中包含以下内容:

    {
      "sdk": {
        "version": "2.0.0"
      }
    }
    

    确保将 version 值设置为教程中的值。

    您可以在the official documentation 中阅读有关此行为的更多信息。

    【讨论】:

    • 感谢您的回答!我知道必须有一个很好的方法来处理这个问题。我发现dotnet new webapi --framework netcoreapp2.1 也可以使用
    猜你喜欢
    • 2012-02-13
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多