visual studio code .net 開發

  Visual Studio確實是至關好用,各類簡化操做什麼的簡直不要太舒服。但其容量太大,有時不是很方便,因此今天簡單介紹一下另外一個工具--Visual Studio Code.linux

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

下面以.Net開發爲例:github

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

 

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

    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包) (推薦)windows

    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:app

{
   // 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文件.另外,這些配置不是固定不變的,你能夠根據你的須要來進行不一樣的配置。工具

如上面ui

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

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


Tasks.json:url

{
    "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

相關文章
相關標籤/搜索