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應用啓動類文件,用於項目啓動前進行相關配置
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章