(精华)2020年12月23日 .NET Core 命令行的基本使用

146 阅读1分钟

一:打开命令行

运行一下:

dotnet new --list

在这里插入图片描述
根据模板创建项目

dotnet new console -n helloword

在这里插入图片描述
那么看一下dotnet core 创建的解决方案helloword.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
</Project>

修改为

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
  </PropertyGroup>
</Project>

执行命令生成exe文件

dotnet build

执行命令启动程序

dotnet run

改造控制台程序变成web执行

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
  </PropertyGroup>
  <ItemGroup>
     <FrameworkReference Include="Microsoft.AspNetCore.App"/>
  </ItemGroup>
</Project>

修改控制台程序

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using System;

namespace helloword
{
    class Program
    {
        static void Main(string[] args)
        {
            WebHost.CreateDefaultBuilder().UseKestrel().Configure(app => app.Run(
                 context => context.Response.WriteAsync("hello word!")
                )).Build().Run();
        }
    }
}

继续启动程序
在这里插入图片描述
这样就实现了web启动