visual studio code .net 開發


  Visual Studio確實是相當好用,各種簡化操作什么的簡直不要太舒服。但其容量太大,有時不是很方便,所以今天簡單介紹一下另一個工具--Visual Studio Code.

雖然相比於老大哥Visual Studio,VS Code有很多功能不完善,但它也更靈活輕便。並且VS Code還在不斷的更新當中,目前的最新版本是18年11月更新的1.30版本,包含了 多行搜索改進 等內容。

下面以.Net開發為例:

不同於Visual Studio,在VS Code上進行.Net開發你需要安裝一些插件,點擊左側邊欄箭頭這個位置搜索

 

安裝插件 C# (C# for Visual Studio Code (powered by OmniSharp)) (必須)

    Lightweight development tools for .NET Core.
    1. Great C# editing support, including Syntax Highlighting, IntelliSense, Go to Definition, Find
    All References, etc.
    Debugging support for .NET Core (CoreCLR). NOTE: Mono debugging is not supported. Desktop CLR debugging has limited support.
    Support for project.json and csproj projects on Windows, macOS and Linux.

安裝插件 NuGet Package Manager (推薦,方便搜索,安裝Nuget包) (推薦)

    Search the NuGet package repository for packages using either (partial or full) package name or another search term.
    Add PackageReference dependencies to your .NET Core 1.1+ .csproj or .fsproj files from Visual Studio Code's Command Palette.
    Remove installed packages from your project's .csproj or .fsproj files via Visual Studio Code's Command Palette.
    Handles workspaces with multiple .csproj or .fsproj files as well as workspaces with single .csproj/.fsproj files.
      1. For example:

 

    1. Ctrl + P Then Shift + > Choose "Nuget Package Manager: Add Package". Then Input the keyword about the Nuget Package. Finally, Choose the project you want to add the Nuget package.

VSCode 安裝插件后一般需要重新激活以啟用插件。由於VSCode本身不斷更新,對於某些版本的VSCode可能需要重啟應用,才能激活插件。
在VSCode中運行調試前,需要在.vscode路徑下額外配置兩個文件 launch.json tasks.json, 來告訴vscode如何啟動項目。
Launch.json:

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceRoot}/MyABP.Web/bin/Debug/netcoreapp2.1/MyABP.Web.dll",
            "args": [],
            "cwd": "${workspaceRoot}/MyABP.Web",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart",
            "launchBrowser": {
                "enabled": true,
                "args": "${auto-detect-url}",
                "windows": {
                    "command": "cmd.exe",
                    "args": "/C start ${auto-detect-url}"
                },
                "osx": {
                    "command": "open"
                },
                "linux": {
                    "command": "xdg-open"
                }
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceRoot}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

如果你第一次運行某個項目,你需要確保所有引用的內容都存在,如上面的MyAbp.Web.dll文件.另外,這些配置不是固定不變的,你可以根據你的需要來進行不同的配置。

如上面

"preLaunchTask": "build" 指定了你的項目在launch之前要先進行build操作。

又如
"env": {
                "ASPNETCORE_ENVIRONMENT": "Development" }
指定了你要在“哪個環境”下啟動你的項目。(實際上一個項目會有多種環境的配置,舉個例子 appsetings.Development.json 和 appseting.QA.json用於區分不同環境的配置,如果發現在QA環境出現了問題本地不能重現時,自然需要切換到目標環境來進行調試)


Tasks.json:

{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "${workspaceRoot}/MyABP.Web/MyABP.Web.csproj"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        }
    ]
}

這里可以配置一下Task,如上面的

"preLaunchTask": "build" 具體的任務流程即在這里配置。

這里除去筆者使用的Web項目的配置外,當然還可以有多種其他項目應用的配置,如

 

 

這些配置完成之后,點擊如圖所示這個位置進入調試面板,然后點擊上面的綠色三角就可以開始你的調試啦

 

/ **************************************************分割線*************************************************************/

vscode下常用DotNet命令
dotnet所使用的命令都可以使用 --help的方式來查看更詳細的用法。
如dotnet --help
在項目開發過程中,常用的命令有

dotnet new
用於新建內容,dotnet提供了許多模板,如Console Application, Class Library等等。
使用dotnet new時需要注意新建的項目是基於.Net Framework還是基於.NetCore.
可以使用 -f 來指定.

dotnet restore

主要是尋找當前目錄下的項目文件(project.json),然后利用NuGet庫還原整個項目的依賴庫,然后遍歷每個目錄,生成項目文件,繼續還原該項目文件中的依賴項 --CSDN yangzhenping

以下命令不解釋了,可以使用 --help 查看具體用法

dotnet build
dotnet run
dotnet publish

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM