Nếu cảm thấy Visual Studio Community quá nặng nề, nhưng bạn vẫn muốn dev website bằng asp.net, và target .net framework, vậy tại sao bạn không sử dụng VSCode nhỉ?

Trớ trêu thay, OmniSharp chưa hỗ trợ .net core 2 trên vscode, nhưng bài viết này sẽ hướng dẫn bạn cách để vượt qua giới hạn đó

Lưu ý là cách này chỉ hoạt động trên windows nhé

1. Chuẩn bị

NẾU BẠN SỬ DỤNG .NET FRAMEWORK 4.6.1

Cài thêm nuget package sau vào project để bỏ các lỗi liên quan tới intellisense

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

2. Các bước cài đặt

2.1. Create project

Open VSCode -> Open Folder -> trỏ tới folder sẽ chứa tất cả code của bạn

Views > Integrated Terminal

rồi gõ

dotnet new mvc

SDK sẽ tạo ra toàn bộ folders và files cần thiết cho bạn

Các lựa chọn khác khi tạo một project: ‘dotnet new’ command at docs.microsoft.com

2.2. chỉnh sửa project để target .NET Framework 4.7.1 (or 4.6.1)

Trong thư mục gốc của project mới tạo, mở file YourProjectName.csproj và sửa như sau

<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 và Debug

  1. Debug > Settings

  1. Chọn .NET Core trong danh sách

  2. Sửa file launch.json trong thư mục .vscode thành như sau

  1. Tạo một file mới cũng trong thư mục đó, với tên là tasks.json với đoạn code sau
{
    "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

Bạn cũng có thể chạy project bằng nút Run trên status bar

Để debug thì chỉ cần đặt break point ngay tại nơi cần debug

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.