經過參照.NET Core相關官方文檔,在個人Mac電腦上用Visual Studio Code建立了個人第一個ASP.NET應用。html
首先要先安裝.Net Core和Visual Studio Code,而且要給Visual Studio Code安裝
C# extension,另外要到nodejs.org
安裝Node.js和npm。node
因爲咱們在安裝Node.js時自帶的npm地址是牆外,npm install有可能沒有反應或者很卡。因此本文全部的npm安裝都使用了淘寶NPM鏡像:git
npm install cnpm -g --registry=https://registry.npm.taobao.org
使用npm安裝必要的yeoman generators和bower。github
sudo cnpm install -g yo generator-aspnet bower
Yeoman的logo是一個戴帽子的男人。它實際上是一個工做流,這個工做流包含了三種用來提高你構建一個Web應用的生產力和滿意度的工具:
腳手架工具(yo), 構建工具(Gulp,Grunt等), 包管理工具(好比npm和Bower)。macos
使用yo aspnet
來運行ASP.NET Core generator,以生成一個Web Application基礎模板。npm
yo aspnet
yo aspnet
生成的模板都是基於你們熟悉的Visual Studio 2015上的模板,這個模板維護在ASP.NET Templates project。json
而後會提示What type of application do you want to create?visual-studio-code
這裏選擇Web Application Basic [without Membership and Authorization]
並回車。瀏覽器
接着會提示Which UI framework would you like to use?bash
這裏選擇Bootstrap (3.3.6) as the UI framework
並回車。
用MyFirstApp
做爲應用名稱並回車。以下圖:
此時generator會生成項目的基礎框架文件,而後提示你分別執行restore,build,run命令。
Your project is now created, you can use the following commands to get going cd "MyFirstApp" dotnet restore dotnet build (optional, build will also happen with it‘s run) dotnet run
至此,打開本地的瀏覽器在地址欄輸入:http://localhost:5000
,便可訪問你建立的第一個程序。
用Visual Studio Code打開剛剛建立的項目,經過快捷鍵⌘⇧P
輸入dot
,選擇dotnet: Restore Packages
來restore必要的build和debug項目依賴。能夠在VS Code中直接運行包括dotnet restore
在內的命令和全部在project.json文件中引用到的工具以及在.vscode/tasks.json
中自定義的任務。
你還能夠經過快捷鍵⌃`調出集成在VS Code裏面的控制檯。
對於未被用到的using
語句會被標記一道綠色波浪線,鼠標移到上面還有顯示一個黃色小燈泡,此時你可使用⌘ .
移除它們;類和方法也會顯示它們在該項目中被引用的次數;還能夠經過⌘KC
來添加代碼塊註釋,經過⌘KU
來取消註釋。
點擊VS Code左側調試窗格中的綠色三角形的調試按鈕,可能會在頂部出現一個錯誤提示信息:
根據錯誤提示可知,咱們須要配置launch.json
文件裏面的program
爲實際的可執行文件。
配置成功後,咱們能夠給程序設置斷點、添加監視等。
最終程序會調出本地默認的瀏覽器程序並導航到http://localhost:5000
,效果以下:
本示例採用Kestrel做爲Web服務器,能夠在project.json裏看到它被做爲一個依賴項。
KestrelHttpServer服務器是微軟推出的惟一一款基於跨平臺網絡庫libuv的跨平臺Web服務器。
經過代碼能夠發現經過調用IWebHostBuilder的UseKestrel擴展方法便可完成對KestrelHttpServer的註冊。
namespace Microsoft.AspNetCore.Hosting { public static class WebHostBuilderKestrelExtensions { public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder) { return hostBuilder.ConfigureServices(services => { services.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>(); services.AddSingleton<IServer, KestrelServer>(); }); } public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder, Action<KestrelServerOptions> options) { return hostBuilder.UseKestrel().ConfigureServices(services => { services.Configure(options); }); } } }