using Microsoft.AspNetCore.Hosting; namespace MyWeb { class Program { static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() // 指定WebServer为Kestrel .UseStartup<Startup>() // 配置WebHost .Build(); host.Run(); // 启动WebHost } } }
新建一个Startup类,用于配置WebHost
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Http; namespace MyWeb { class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.Run(c => { return c.Response.WriteAsync("Hello dotnet core."); }); } } }5.启动我们这个干净的Web应用程序 6.访问网站 7.让网站的响应能力强一点
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Http; namespace MyWeb { class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.Run(c => { var req = c.Request.Path.ToString().TrimStart('/'); var res = string.Empty; switch (req) { case "1": res = "one"; break; case "2": res = "two"; break; default: res = "none"; break; } var mtd = string.Empty; switch (c.Request.Method) { case "GET": mtd = "请求方式: get"; break; case "POST": mtd = "请求方式:post"; break; default: mtd = "请求方式:none"; break; } return c.Response.WriteAsync(res); }); } } }logs_code_collapse">View Code 效果如下: 至此,我们使用Asp.Net Core 2.0创建了一个最小架构的Web应用程序。MVC、WebAPI是在此基础上的更高层次的封装。
我们并没有看到我们使用Nuget应用的两个包,这也说明.NetCore2.0做了运行时包仓优化。
参考:02、NetCore2.0优化之Nuget包