起步
- Install .NET Core
- 安装 .NET Core
- Create a new .NET Core project:
创建一个新的 .NET Core 项目 mkdir aspnetcoreapp cd aspnetcoreapp dotnet new
Update the project.json file to add the Kestrel HTTP server package as a dependency:
更新 project.json 文件,添加 Kestrel HTTP 服务器包作为一个依赖 {
"version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" }, "Microsoft.AspNetCore.Server.Kestrel": "1.0.0" }, "imports": "dnxcore50" } }
}
Restore the packages:
还原包 dotnet restore
Add a Startup.cs file that defines the request handling logic:
添加一个定义处理请求逻辑的 Startup.cs文件 using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; namespace aspnetcoreapp {
public class Startup { public void Configure(IApplicationBuilder app) { app.Run(context => { return context.Response.WriteAsync("Hello from ASP.NET Core!"); }); } }
}
Update the code in Program.cs to setup and start the Web host:
6.更新 Program.cs文件中的代码来设置和启动 Web 托管:
using System; using Microsoft.AspNetCore.Hosting; namespace aspnetcoreapp { public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseStartup<Startup>() .Build(); host.Run(); } } }
- Run the app (the dotnet run command will build the app when it’s out of date):
7.启动应用(dotnet run
命令会在应用过期的时候重新生成)
dotnet run
- Browse to http://localhost:5000:
8.打开浏览器并浏览 http://localhost:5000