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.4 (2024年4月9日;稳定版本)[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 的项目管理方式,但仍保留以目录为主的项目管理作法。

v1.0 时期 编辑

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英语Yeoman (computing) for ASP.NET,提供给 Mac 与 Linux 等操作系统,可直接产生项目模板的工具。
  • 只要是文本编辑器都可以编修 project.json 与代码文件,但要自行处理构建与部署的细节。

版本历程 编辑

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

引用 编辑

  1. ^ Release 8.0.4. 2024年4月9日 [2024年4月19日]. 
  2. ^ 2.0 2.1 ASP.NET 5 is dead – Introducing ASP.NET Core 1.0 and .NET Core 1.0. [2016-03-07]. (原始内容存档于2016-03-08). 
  3. ^ Introducing ASP.NET vNext. [2016-03-07]. (原始内容存档于2016-03-10). 
  4. ^ ASP.NET Core 1.0 Schedule and Roadmap. [2016-03-07]. (原始内容存档于2016-02-22). 
  5. ^ OWIN. [2016-03-07]. (原始内容存档于2016-03-09). 
  6. ^ Understanding OWIN Forms authentication in MVC 5. [2016-03-07]. (原始内容存档于2016-03-07). 
  7. ^ Dependency Injection in ASP.NET vNext. [2016-03-07]. (原始内容存档于2016-03-07). 
  8. ^ ASP.NET Core Dependency Injection. [2016-03-07]. (原始内容存档于2016-02-09). 
  9. ^ ASP.NET Core Servers. [2016-03-07]. (原始内容存档于2015-05-28). 
  10. ^ 10.0 10.1 Announcing Availability of ASP.NET 5 Beta8. [2016-03-07]. (原始内容存档于2016-03-08). 
  11. ^ Project.json file. [2016-03-07]. (原始内容存档于2016-02-05). 
  12. ^ Introducting ASP.NET vNext. [2016-03-07]. (原始内容存档于2016-03-10). 
  13. ^ GitHub Release History: 1.0.0-alpha2. [2016-03-08]. (原始内容存档于2017-04-07). 
  14. ^ GitHub Release History: 1.0.0-alpha3. [2016-03-08]. (原始内容存档于2017-04-07). 
  15. ^ GitHub Release History: 1.0.0-alpha4. [2016-03-08]. (原始内容存档于2017-04-07). 
  16. ^ GitHub Release History: 1.0.0-beta1. [2016-03-08]. (原始内容存档于2017-04-07). 
  17. ^ GitHub Release History: 1.0.0-beta2. [2016-03-08]. (原始内容存档于2017-04-07). 
  18. ^ GitHub Release History: 1.0.0-beta3. [2016-03-08]. (原始内容存档于2017-04-07). 
  19. ^ GitHub Release History: 1.0.0-beta4. [2016-03-08]. (原始内容存档于2017-04-07). 
  20. ^ ASP.NET 5 Beta5 Now Available. [2016-03-08]. (原始内容存档于2016-03-08). 
  21. ^ Announcing Availability of ASP.NET 5 beta6. [2016-03-08]. (原始内容存档于2016-03-08). 
  22. ^ Announcing Availability of ASP.NET 5 Beta7. [2016-03-08]. (原始内容存档于2016-03-08). 
  23. ^ GitHub Release History: 1.0.0-rc1-final. [2016-03-08]. (原始内容存档于2017-04-07). 
  24. ^ ASP.NET Core RC2. [2016-05-16]. (原始内容存档于2017-04-07). 
  25. ^ Announcing ASP.NET Core 1.0. [2016-06-28]. (原始内容存档于2016-06-28). 
  26. ^ ASP.NET Core, .NET Core, EF Core 1.0.1 Updates. [2017-03-20]. (原始内容存档于2017-03-20). 
  27. ^ ASP.NET Core 1.1.0 Preview 1. [2017-03-20]. (原始内容存档于2019-09-19). 
  28. ^ Announcing the Fastest ASP.NET Yet, ASP.NET Core 1.1 RTM. [2017-03-20]. (原始内容存档于2017-03-20). 
  29. ^ Announcing ASP.NET Core 1.0. [2017-03-20]. (原始内容存档于2019-09-19). 
  30. ^ Announcing ASP.NET Core 2.0.0 Preview 1. [2017-05-11]. (原始内容存档于2017-07-10). 
  31. ^ Announcing ASP.NET Core 2.0. [2017-08-14]. (原始内容存档于2017-08-14). 
  32. ^ GitHub - dotnet/core: Home repository of .NET and .NET Core. October 20, 2019 [2022-09-20]. (原始内容存档于2016-02-11) –通过GitHub. 
  33. ^ ASP.NET Blog | Announcing ASP.NET Core 2.2, available today!. ASP.NET Blog. December 4, 2018 [2022-09-20]. (原始内容存档于2021-10-04). 
  34. ^ ASP.NET Blog | ASP.NET Core and Blazor updates in .NET Core 3.0. ASP.NET Blog. September 23, 2019 [2022-09-20]. (原始内容存档于2021-10-21). 
  35. ^ ASP.NET Core updates in .NET Core 3.1. ASP.NET Blog. December 3, 2019 [2022-09-20]. (原始内容存档于2021-10-21). 
  36. ^ dotnet/aspnetcore, .NET Platform, 2020-11-11 [2020-11-11], (原始内容存档于2020-01-06) 
  37. ^ Announcing ASP.NET Core in .NET 6. .NET Blog. 2021-11-08 [2021-11-19]. (原始内容存档于2020-11-28) (美国英语).