Reputation: 169
Hi I'm following instructions from a tutorial. Using dotnet new webapi
I'm getting a different project than the lesson shows and from another person's computer which I tested this on. We get mostly the same files but I have one extra WeatherForcast.cs
and instead of the ValuesController.cs
I get WeatherForcastController.cs
with completely different code in it that I'll post at the bottom.
There are also some smaller differences in Program.cs
and Startup.cs
What accounts for this difference? How do I generate the files the way I'm trying, from the examples?
dotnet new webapi --framework netcoreapp2.2
gives me the version of this webapi I'm looking for.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)
{
}
}
}
Upvotes: 1
Views: 2317
Reputation: 2887
As was already pointed out in other answers, this is due to the difference in the SDKs being used.
Even if you have the same SDK installed, but have a newer one installed too, the newer SDK will be used by default.
You can, however, pin the SDK version to be used by the dotnet new
command by adding a global.json
file in the directory, where from you're running the command, with the following content:
{
"sdk": {
"version": "2.0.0"
}
}
Make sure to set the version
value to the one from the tutorial.
You can read more about this behavior in the official documentation.
Upvotes: 3
Reputation: 300
Because You have .net core 3.x SDK version on your computer and Other Computers that creating Values Controller have 2.x SDK.
Upvotes: 1
Reputation: 11703
It's based on the version of the .NET Core SDK you have installed. If you want to follow the tutorial exactly, install that particular version. And get rid of any version that is newer. After you have learned from the tutorial, install the latest.
Upvotes: 1