你的位置:
首页
>
软件开发 >
ASP.net > [ASP.NET Core] Getting Started
[ASP.NET Core] Getting Started
发布时间:2016-10-17 18:00:12
[ASP.NET Core] Getting Started前言本篇文章介绍如何快速建立一个ASP.NET Core应用程序,为自己留个纪录也希望能帮助到有需要的开发人员。ASP.NET Core官网环境建立一个ASP.NET Core应用程序,首先要从官网下载SDK来建置.NE ...
[get='_blank'>ASP.NET Core] Getting Started
前言
本篇文章介绍如何快速建立一个ASP.NET Core应用程序,为自己留个纪录也希望能帮助到有需要的开发人员。
环境
建立一个ASP.NET Core应用程序,首先要从官网下载SDK来建置.NET Core开发环境。
开发
完成开发环境的建置后,就可以动手撰写ASP.NET Core应用程序。首先建立一个新的文件夹:「lab」。
接着在lab文件夹里,加入一个档案:「project.json」。并且修改档案内容为下列json格式内容,用以设定ASP.NET Core应用程序的项目参数。
{ "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" } }}
接着同样在lab文件夹里,加入一个档案:「Program.cs」。并且修改档案内容为下列C#程序代码内容,用以做为ASP.NET Core应用程序的范例程序。
using System;using System.Threading.Tasks;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Builder;namespace aspnetcoreapp{ public class Program { public static void Main(string[] args) { // Build var host = new WebHostBuilder() // 设定启动参数 .UseStartup<Startup>() // 开启Kestrel聆听HTTP .UseKestrel() // 设定聆听的URL .UseUrls("http://localhost:5000") // 建立Host .Build(); // Run try { // 启动Host host.Start(); // 等待关闭 Console.WriteLine("Application started. Press any key to shut down."); Console.ReadKey(); } finally { // 关闭Host host.Dispose(); } } } public class Startup { // Methods public void Configure(IApplicationBuilder app) { // 挂载自定义的Middleware app.UseMiddleware<HelloWorldMiddleware>(); } } public class HelloWorldMiddleware { // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public Task Invoke(HttpContext context) { return context.Response.WriteAsync("Hello World!"); } }}
再来开启命令提示字符,进入到上述的lab文件夹后。输入「dotnet restore」,用以初始化ASP.NET Core应用程序。
初始化ASP.NET Core应用程序后,接着输入「dotnet run」,用以编译并执行ASP.NET Core应用程序。
执行
参考
ASP.NET Core Getting Started - ASP.NET
ASP.NET Core 1.0 Hello World - 小朱® 的技术随手写
ASP.NET Core 的 Middleware - ASP.NET Core 信息分享
原标题:[ASP.NET Core] Getting Started
关键词:ASP.NET
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。