ASP.NET Core 入门学习笔记 1、使用ASP.NET Core 构建第一个Web应用

在此感谢博主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应用启动类文件,用于项目启动前进行相关配置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值