经过参照.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); }); } } }