在此感谢博主ken的杂谈,参照该博主文章记录自己的学习
博主文章链接:博主链接
一、前置知识的学习
1、VS Code + .NET Core快速开始
2、C#语法学习
此类知识应当是学习.NET Core 的基础知识
主要通过菜鸟课程(菜鸟)去看和复习
二、环境安装
1、SDK的安装
下载地址
由于自己电脑之前学习已经装过最新版,故跳过了此步骤
2、VS Code下载&安装
下载地址
三、学习过程
1、项目创建
通过命令行创建项目
#创建项目目录
mkdir projects
#进入项目目录
cd projects
#创建项目
dotnet new web -n helloweb
注:尝试用vs2017打开,此并不会提示所缺少的包等,但会报依赖项的缺失。
2、用博主说的vs code打开
打开后会在软件右下方提示所缺失的包
直接选择yes确定安装缺失的包即可
打开后可直接按F5启动项目
3、修改绑定协议HTTPS为HTTP
打开Properties/launchSettings.json文件
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53122",
"sslPort": 44309
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"helloweb": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
将applicationUrl修改为http://localhost:5001
然后重启项目(Ctrl+Shift+F5)机会看到干净纯洁的Hello World!
4、发现自己所生成的初始项目和博主的不大一样
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace helloweb
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
在应用启动的时候,会执行CreateHostBuilder方法,在这个方法中通过类Startup创建了默认了HostBuilder
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace helloweb
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
}
ConfigureServices 用于配置应用启动时加载的Service
Configure 用于配置HTTP请求管道
web项目模板默认在项目启动的时候调用IApplicationBuilder.run方法,在当前HTTP上下文(HttpContext)中输出了Hello World!
context.Response.WriteAsync(“Hello World!”);
四、备注
1、项目结构说明
根目录/文件 | 说明 |
---|---|
.vscode目录 | VS Code项目配置目录,相当于.vs、.idea文件夹 |
bin目录 | 编译输出目录,相当于Java项目的target目录 |
obj目录 | 编译配置与中间目录,用于存放编译配置与编译中间结果 |
Properties目录 | 用于存放项目配置 |
wwwroot目录 | 静态文件目录 |
helloweb.csproj文件 | 项目描述文件 |
Program.cs文件 | 应用程序入口类文件 |
Startup.cs文件 | ASP.NET Core Web应用启动类文件,用于项目启动前进行相关配置 |