0

Using MS Visual Studio 2019. Need to create a .NET Core 2 MVC Solution. I need to target .NET Core 2 and not 3 because I need to use some libraries that, unfortunately, doesn't target .NET Core 3.

Is there any way to use Visual Studio 2019 for this, or dotnet new, without using a template that targets 3?

HelloWorld
  • 3,381
  • 5
  • 32
  • 58

1 Answers1

0

Make sure you have to 2.x SDK installed, CLI command dotnet --list-sdks

  1. Create a new ASP.NET core webapp project from Visual Studio or CLI --> dotnet new webapp -o myapp
  2. Update your .csproj something like this:
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.7" />
  </ItemGroup>

</Project>
  1. Update program.cs to use the older IWebHostBuilder
 public class Program
 {
    public static void Main(string[] args)
        => BuildWebHost(args).Run();


    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        => WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseConfiguration(Configuration)
            .Build();
 }
  1. I think you need to add this for razor pages.
// ConfigureServices(IServiceCollection services)
services.AddMvc().AddRazorPagesOptions(async  (opt)  => await Task.CompletedTask);

// Configure(IApplicationBuilder app, IHostEnvironment env)
app
    .UseStaticFiles()
    .UseDefaultFiles();

app.UseMvc()

I'm not sure about the razor pages stuff in #4, never used it. But this should probably work.

Here are some guides:

Henkolicious
  • 1,273
  • 2
  • 17
  • 34