ASP.NET Core

ASP.NET Core 是新一代的 ASP.NET,早期称为 ASP.NET vNext,并且在推出初期命名为 ASP.NET 5,但随着 .NET Core 的成熟,以及 ASP.NET 5 的命名会使得外界将它视为 ASP.NET 的升级版,但它其实是新一代从头开始打造的 ASP.NET 内核功能,因此微软宣布将它改为与 .NET Core 同步的名称[2],即 ASP.NET Core。

ASP.NET Core
原作者Microsoft
开发者.NET Foundation and the open source community
当前版本
  • 8.0.3 (2024年3月12日;稳定版本)[1]
源代码库
编程语言C#
操作系统Windows, macOS, Linux
类型开放原代码、Web应用程序框架、MVC
许可协议MIT License
网站门户网站GitHub文档库工具

ASP.NET Core 可运行于 Windows 平台以及非 Windows 平台,如 Mac OSX 以及 Ubuntu Linux 操作系统,是 Microsoft 第一个具有跨平台能力的 Web 开发框架。

微软在一开始开发时就将 ASP.NET Core 开源,因此它也是开源项目的一员,由.NET 基金会页面存档备份,存于 (.NET Foundation) 所管理。

开发历程

ASP.NET vNext 最早是在2014年5月被提出[3],当时是以项目代号 K (Project K) 命名,包含工具与运行期平台都以 K 来命名,其名称为 KRE (K Runtime Environment) 如:

  • KLR (K Language Runtime): Project K 的运行期平台。
  • KVM (K Version Manager): Project K 的版本管理员。
  • KPM (K Package Manager): Project K 的套件管理员,类似于 node.js 的 npm (Node.js Package Manager)。
  • K (K Loader): Project K 的程序启动器。

KRE 在 2014 年度时曾经一度在版本戳记上出现 RC 的字眼,但随后 ASP.NET Core 开发团队发出了一份新的 Roadmap,公布后续的版本计划 [4]

到了 Build 2015 时,微软将项目 K 重命名,改称为 .NET 运行环境 (.NET Execution Environment),简称 DNX,其工具也开始进行更名:

  • DNVM (.NET Version Manager): DNX 的版本管理员。
  • DNU (.NET Package Manager): DNX 的工具,可支持建造,还原与管理封装等。
  • DNX (.NET Loader): DNX 的程序启动器。

后续到了 ASP.NET Core RC1 时,.NET Core 已经发展成熟,并且也具有自己的工具 .NET Core CLI (dotnet.exe),此时微软决定将 DNX 和 .NET Core CLI 合并,并且正式于 ASP.NET Core 1.0.0 RC2 时将 DNX 支持终止,改由 .NET Core CLI 提供基础的编译与运行功能。

内核功能

ASP.NET Core 内核设计上是采用 Open Web Interface for .NET (OWIN) [5] 为概念发展,OWIN 在概念上就强调以代码来定义系统功能,并一度在 ASP.NET MVC 5 列入其功能之一[6],后续的 Web API 与 SignalR 也使用了 OWIN,但并没有引起太多开发人员的重视,其主因还是因为 Visual Studio 简化了太多组件间参考定义的工作,若是要回归由原代码作业,反而会让开发人员无法适应。但随着微软确定将 ASP.NET Core 开发为可跨平台的内核架构时,其项目参考系统也由 Visual Studio 为主的加入参考对话盒转向到以 project.json (.NET Core / ASP.NET Core 的项目组态档) 为主,使得开发人员不能再以 GUI 接口来加入组件参考,只能利用编辑 project.json 的方式加入,这时由代码加入功能的作法才慢慢的被开发人员所接受,虽然这在 Mac 以及 Linux 环境是再平常不过的事。

由代码决定功能

ASP.NET Core 广泛应用了 .NET 的扩充方法 (Extension Method),将 ASP.NET Core 的功能模块 (ASP.NET Core 的术语是 Middleware) 以扩充方法的方式附挂在 IApplicationBuilder 接口上,以 Use 开头的方法为命名标准,所有应用程序所需要的功能都必须添加在 Startup 类别内,DNX Runtime 在启动时会搜索应用程序内的 Startup 对象,并唤起它内部的特定方法 (如 Configure()),以加入应用程序的功能。

例如下列程序代码:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseBrowserLink();
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

    app.UseStaticFiles();

    app.UseIdentity();

    // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

内置的相依注入

ASP.NET Core 内核内置了基本的相依注入 (Dependency Injection) 能力,这意味着 ASP.NET Core 应用程序具有更强的扩充能力,连带的如 ASP.NET Core MVC (ASP.NET Core 的 Web 开发框架) 也受惠于 ASP.NET Core 的相依注入功能,可在 Controller 内直接注入接口对象。ASP.NET Core 内提供了两种相依注入功能,一种是管线式相依注入 (ASP.NET Core 称它为 Framework-Provided Services),另一种是由系统注册的相依注入,由开发人员利用 ASP.NET Core 提供的 IServiceCollection 接口内的方法注册需要的接口与服务,再由程序中取用。

IServiceCollection 支持四种类型的服务相依注入[7]

  1. Transient: 每次要求时都创建,不论是否在同一范围。
  2. Scoped: 只在一个要求范围内置立一次,在当下的范围内等同于 Singleton。
  3. Singleton: 只会提供一个对象的运行个体,但生成是由系统做。
  4. Instance: 在应用程序的生命周期内只会提供一个对象的运行个体,但生成是由开发人员负责。

ASP.NET Core 也不限只能用 ASP.NET Core 本身的相依注入功能,若是有喜欢或惯用的相依注入组件 (如 AutofacStructureMap 等),也可以用它们取代内置的相依注入功能[8]

代管 (Hosting) 方式

ASP.NET Core 支持自我代管 (Self-hosting) 以及 Web Server 代管 (Web Server-hosting) 的功能 [9],早期 ASP.NET Core 有延续以 IIS 为主要代管服务的设计 (当时的代号为 Helios),但是到了 ASP.NET Core Beta 8 时,微软宣布将以 Kestrel Server 为主要的代管服务器 [10],Kestrel Server 是以 libuv 为基础开发的 Web Server 代管行程 (Hosting Process),借由 libuv 的协助,Kestrel Server 可跨平台,也可适用于 IIS,微软也为了 IIS 使用 Kestrel Server 而发展了 IIS Platform Handler,让 IIS 可直接将 HTTP 的要求直接转送给 Kestrel Server。

自 ASP.NET Core 1.0.0-rc1 起,代管方式已经回归以 Kestrel Server 为主,原本的 IIS Platform Handler 也依 ASP.NET Core 的特性改写为 ASP.NET Core Module,若要使用 IIS 架设 ASP.NET Core 应用程序,必须要使用此模块。

项目系统

在 ASP.NET Core v1.0 时期,ASP.NET Core 不再使用 .csproj 的项目管理方式,而是改用以目录为主的项目管理,原本的 Web.config 也不再存在,取而代之的是 project.json,以及作为组态设置的 appsettings.json 文件,这两个文件都是JSON格式。虽然在 Visual Studio 的 ASP.NET Core 的项目范本中,Web.config 仍然存在,但它的存在只是为了要在 IIS 中附挂上 HTTP Platform Handler 而已。

随着 .NET Core v1.1 回归到 MSBuild 的策略,ASP.NET Core v1.1 起再次回到 .csproj 的项目管理方式,但仍保留以目录为主的项目管理作法。

project.json

ASP.NET Core v1.0 时期由 project.json 主掌项目的运行期的组态设置,包含项目的套件参考 (Package References)、项目的基本设置、启动指令、包含或排除指定目录、以及建造时的相关事件指令等。

下列JSON为 project.json 的范例[11]

{
  "userSecretsId": "...",

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002538",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-20828",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-20828",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0-rc2-20828",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0-rc2-20828",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-20828",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-20828",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-20828",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-20828",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-20828",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-20828",
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-20828",
      "type": "build"
    },
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-20828",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-20828",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-20828",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-20828",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-20828",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-20828",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-20828",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-20828",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview1-20828",
      "type": "build"
    }
  },

  "tools": {
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-20828",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-20828",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-20828",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    },
    "Microsoft.Extensions.SecretManager.Tools": {
      "version": "1.0.0-preview1-20828",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview1-20828",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

appsettings.json

appsettings.json 是用来替代 Web.config 内的 <appSettings /> 与 <connectionStrings /> 两个开发人员最常用的组态区段,其内容示例如下:

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-WebApplication1-8479b9ce-7b8f-4402-9616-0843bc642f09;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Verbose",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

不过 appsettings.json 不像 project.json 是由 DNX 自动读取,appsettings.json 或是后续加入的组态档都是属于功能之一,依照 ASP.NET Core 由代码决定功能的特性,开发人员需要加入下列程序才能让 appsettings.json 生效。

public Startup(IHostingEnvironment env)
{
    // Set up configuration providers.
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    Configuration = builder.Build();
}

v1.1 时期

随着 .NET Core 1.1 回归 MSBuild 建置系统管理后,ASP.NET Core 1.1 的套件管理也可支持 IDE GUI 的图形化接口参考管理功能。

ASP.NET Core 1.1 的 Visual Studio 工具也另外加入了 Bower 套件的图形化接口管理功能。

套件相依管理

ASP.NET Core 的套件相依管理 (Package Dependency Management) 由 project.json 负责,project.json 内的 dependencies 区段以及 frameworks 区段负责管理对套件的相依,以及对特定 Framework 版本内组件的相依。

dependencies 内的套件是以 "套件代码": "版本" 的格式设置,例如 "Microsoft.AspNet.Mvc": "1.0.0-rc1-final" 表示参考 Microsoft.AspNet.Mvc 套件的 v1.0.0-rc1-final 版本。套件与版本信息是用NuGet所提供,和以往要以加入参考的方式加入对特定组件的相依性有很大的不同,而这也是 .NET Core/ASP.NET Core 的特色之一,不必再安装大包的 .NET Framework,只要使用 DNU 的 restore 指令,就能还原所参照的相依套件。

  "dependencies": {
    "Microsoft.Bcl.Immutable": "1.1.18-beta-*",
    "Microsoft.AspNet.ConfigurationModel": "0.1-alpha-*",
    "Microsoft.AspNet.DependencyInjection": "0.1-alpha-*",
    "Microsoft.AspNet.Logging": "0.1-alpha-*",
    "System.Data.Common": "0.1-alpha-*"
  }

frameworks 则是定义了特定系统环境内所相依的特定组件与其版本,在此定义的组件必须事先就安装在电脑内才能取用,与 dependencies 会还原套件不同。开发人员可以给定一个字符串来代表特定环境 (例如dnx45表示 DNX on .NET 4.5、dnxcore5 表示 DNX on .NET Core 5),然后在里面指定特定的组件与其版本。

  "frameworks": {
    "net451": { },
    "dotnet5.4": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Runtime": "4.0.21-beta-23516",
        "System.Threading": "4.0.11-beta-23516"
      }
    }
  }

自 1.0.0-rc2 起,DNX 的功能由 .NET Core CLI 取代,因此还原套件的指令改为 dotnet restore。

组件

ASP.NET Core 以 .NET Core 的基础发展,其目前的组件有:

  1. ASP.NET Core MVC : 目前钦定的 Web 应用程序开发框架。
  2. ASP.NET Core SignalR: 新一代的长时轮询 (Long-Time Polling) 消息通信基础建设,计划在 ASP.NET Core 1.0.0 发布后才会继续进行开发。
  3. Entity Framework Core: 下一代的 ADO.NET Entity Framework,采用 .NET Core 并重新设计,为钦定的 ORM 数据访问技术。
  4. Identity Core
  5. Razor Core
  6. Blazor
  7. Kestrel web server

开发工具

ASP.NET Core 的可用开发工具有:

  • Visual Studio,内置有 ASP.NET Core 的项目范本,也能够使用 IDE 的功能建造与部署应用程序 (例如 Azure Web App 或是 ASP.NET Docker Image on Azure 等)。
  • Visual Studio Code,跨平台的 Visual Studio 编辑器。
  • Yeoman Generator for ASP.NET,提供给 Mac 与 Linux 等操作系统,可直接产生项目范本的工具。
  • 只要是文本编辑器都可以编修 project.json 与代码文件,但要自行处理建造与部署的细节。

版本历程

版本历程
日期版本
2014/05/12ASP.NET 5 初登场 [12]
2014/07/06ASP.NET 5 Alpha2 [13]
2014/08/17ASP.NET 5 Alpha3 [14]
2014/10/07ASP.NET 5 Alpha4 [15]
2014/11/12ASP.NET 5 Beta1 [16]
2015/01/14ASP.NET 5 Beta2 [17]
2015/03/13ASP.NET 5 Beta3 [18]
2015/05/01ASP.NET 5 Beta4 [19]
2015/06/30ASP.NET 5 Beta5 [20]
2015/07/27ASP.NET 5 Beta6 [21]
2015/09/02ASP.NET 5 Beta7 [22]
2015/10/15ASP.NET 5 Beta8 [10]
2015/11/19ASP.NET 5 RC1 [23]
2016/01/19ASP.NET 5 更名为 ASP.NET Core [2]
2016/05/16ASP.NET Core 1.0.0 RC2 [24]
2016/06/27ASP.NET Core 1.0.0 RTM [25]
2016/09/14ASP.NET Core 1.0.1[26]
2016/10/25ASP.NET Core 1.1.0 Preview 1[27]
2016/11/16ASP.NET Core 1.0.0 RTM [28]
2017/03/08ASP.NET Core 1.1.1 RTM [29]
2017/05/18ASP.NET Core 2.0.0-preview1 [30]
2017/08/14ASP.NET Core 2.0.0 RTM [31]
2018-05-30ASP.NET Core 2.1.0 [32]
2018-12-04ASP.NET Core 2.2.0 [33]
2019-09-23ASP.NET Core 3.0.0 [34]
2019-12-03ASP.NET Core 3.1.0 [35]
2020-11-10ASP.NET Core 5.0.0 [36]
2021-11-08ASP.NET Core 6.0.0 [37]

参考

  1. . 2024年3月12日 [2024年3月19日].
  2. . [2016-03-07]. (原始内容存档于2016-03-08).
  3. . [2016-03-07]. (原始内容存档于2016-03-10).
  4. . [2016-03-07]. (原始内容存档于2016-02-22).
  5. . [2016-03-07]. (原始内容存档于2016-03-09).
  6. . [2016-03-07]. (原始内容存档于2016-03-07).
  7. . [2016-03-07]. (原始内容存档于2016-03-07).
  8. . [2016-03-07]. (原始内容存档于2016-02-09).
  9. . [2016-03-07]. (原始内容存档于2015-05-28).
  10. . [2016-03-07]. (原始内容存档于2016-03-08).
  11. . [2016-03-07]. (原始内容存档于2016-02-05).
  12. . [2016-03-07]. (原始内容存档于2016-03-10).
  13. . [2016-03-08]. (原始内容存档于2017-04-07).
  14. . [2016-03-08]. (原始内容存档于2017-04-07).
  15. . [2016-03-08]. (原始内容存档于2017-04-07).
  16. . [2016-03-08]. (原始内容存档于2017-04-07).
  17. . [2016-03-08]. (原始内容存档于2017-04-07).
  18. . [2016-03-08]. (原始内容存档于2017-04-07).
  19. . [2016-03-08]. (原始内容存档于2017-04-07).
  20. . [2016-03-08]. (原始内容存档于2016-03-08).
  21. . [2016-03-08]. (原始内容存档于2016-03-08).
  22. . [2016-03-08]. (原始内容存档于2016-03-08).
  23. . [2016-03-08]. (原始内容存档于2017-04-07).
  24. . [2016-05-16]. (原始内容存档于2017-04-07).
  25. . [2016-06-28]. (原始内容存档于2016-06-28).
  26. . [2017-03-20]. (原始内容存档于2017-03-20).
  27. . [2017-03-20]. (原始内容存档于2019-09-19).
  28. . [2017-03-20]. (原始内容存档于2017-03-20).
  29. . [2017-03-20]. (原始内容存档于2019-09-19).
  30. . [2017-05-11]. (原始内容存档于2017-07-10).
  31. . [2017-08-14]. (原始内容存档于2017-08-14).
  32. . October 20, 2019 [2022-09-20]. (原始内容存档于2016-02-11) GitHub.
  33. . ASP.NET Blog. December 4, 2018 [2022-09-20]. (原始内容存档于2021-10-04).
  34. . ASP.NET Blog. September 23, 2019 [2022-09-20]. (原始内容存档于2021-10-21).
  35. . ASP.NET Blog. December 3, 2019 [2022-09-20]. (原始内容存档于2021-10-21).
  36. , .NET Platform, 2020-11-11 [2020-11-11], (原始内容存档于2020-01-06)
  37. . .NET Blog. 2021-11-08 [2021-11-19]. (原始内容存档于2020-11-28) (美国英语).


This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.