[ASP.NET Core 2.0] - dùng VSCode và target .NET Framework

If Visual Studio Community feels too heavy but you still want to build websites with asp.net targeting the .net framework, why not use VSCode?

Ironically, OmniSharp doesn’t support .net core 2 in vscode yet, but this post shows you how to get past that limitation

Note that this only works on windows

1. Preparation

IF YOU USE .NET FRAMEWORK 4.6.1, also install the following nuget package in the project to get rid of the intellisense-related errors

dotnet add package NETStandard.Library.NETFramework --version 2.0.0-preview2-25405-01

2. The setup steps

2.1. Create project

Open VSCode -> Open Folder -> point at the folder that will hold all your code. Views > Integrated Terminal, then type

dotnet new mvc

The SDK creates all the folders and files you need. Other options when creating a project: ‘dotnet new’ command at docs.microsoft.com

2.2. editing the project to target .NET Framework 4.7.1 (or 4.6.1)

In the root folder of the newly created project, open the YourProjectName.csproj file and edit it like this

<Project Sdk="Microsoft.NET.Sdk.Web">
 
  <PropertyGroup>
    <TargetFramework>net471</TargetFramework>
    <!--RuntimeIdentifier is based on your local machine. A list of all available values here-->
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
  </PropertyGroup>
 
  <ItemGroup>
    <!--These are nuget package targeting .net framework, dependency is .NET Standard 2.0, which is supported by .net framework 4.7.1-->
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.1" PrivateAssets="All" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.1" />
  </ItemGroup>
 
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
  </ItemGroup>
 
</Project>

2.3. Compile, Run and Debug

  1. Debug > Settings

  1. Pick .NET Core in the list

  2. Edit the launch.json file in the .vscode folder like this

  1. Create a new file in that same folder named tasks.json with the following code
{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/StudyAspCore.csproj"
            ],
            "problemMatcher": "$msCompile"
        }
    ]
}

2.4. Run and Debug

To run the application

You can also run the project with the Run button in the status bar

To debug, just put a break point exactly where you need to debug