ASP.NET Core (二):入门

上一篇:ASP.NET Core(一):简介

下一篇:(待续)

英文原版:Getting Started

1、 安装 .NET Core

2、 创建 .NET Core 项目

在命令提示符窗口输入命令:

mkdir aspnetcoreapp

cd aspnetcoreapp

dotnet new

3、 更新 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"
    }
  }
}

4、 还原程序包

在命令提示符窗口输入命令:

dotnet restore

5、 添加 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!");
            });
        }
    }
}

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();
        }
    }
}

7、 运行应用程序(app)(如果dll已过期,dotnet run 命令将重新build应用程序)

dotnet run

8、 在浏览器中浏览:http://localhost:5000

ASP.NET Core 应用程序入门效果图

上一篇:ASP.NET Core(一):简介

下一篇:(待续)

                                                 

原文地址:https://www.cnblogs.com/ideck/p/asp_net_core_2.html