添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

ASP.NET Core 将 ASP.NET 4.x 的 MVC 和 Web API 应用模型组合到被称为 ASP.NET Core MVC 的单一编程模型中。

本文介绍如何使用 ASP.NET Web API 2 在 入门 中创建的 Products 控制器迁移到 ASP.NET Core。

Visual Studio Visual Studio Code
  • .NET 6.0 SDK
  • Visual Studio Code 说明使用用于 ASP.NET Core 的 .NET CLI 开发功能,如项目创建。 可在(macOS、Linux 或 Windows)上或在任何代码编辑器中遵循这些说明。 如果使用 Visual Studio Code 以外的其他内容,则可能需要进行少量更改。

    创建新的 ASP.NET Core Web API 项目

    Visual Studio Visual Studio Code
  • 在搜索框中输入“Web API”。
  • 选择“ASP.NET Core Web API”模板,然后选择“下一步”。
  • “配置新项目 ”对话框中,将项目命名为 ProductsCore ,然后选择“ 下一步 ”。
  • 在“其他信息”对话框中:
    1. 确认“框架”为“.NET 6.0 (长期支持)”。
    2. 确认已选中“使用控制器(取消选中以使用最小 API)”。
    3. 取消选中“启用 OpenAPI 支持”。
    4. 选择“创建”。
    5. 运行以下命令以创建新的 Web API 项目,并在Visual Studio Code中将其打开:

      dotnet new webapi -o ProductsCore --no-openapi
      cd ProductsCore
      code -r ../ProductsCore
      

      删除 WeatherForecast 模板文件

    6. WeatherForecast.cs从新的 ProductsCore 项目中删除 和 Controllers/WeatherForecastController.cs 示例文件。
    7. 打开 Properties\launchSettings.json。
    8. 将属性从 weatherforcast 更改为 productscorelaunchUrl
    9. ASP.NET Core Web API 的配置

      ASP.NET Core 不使用 App_Start 文件夹或 Global.asax 文件。 web.config文件在发布时添加。 有关详细信息,请参阅 web.config 文件

      Program.cs 文件:

    10. 替换 Global.asax。
    11. 处理所有应用启动任务。
    12. 有关详细信息,请参阅 ASP.NET Core 中的应用启动

      下面显示了 ASP.NET Core Program.cs 文件中的应用程序启动代码:

      var builder = WebApplication.CreateBuilder(args);
      // Add services to the container.
      builder.Services.AddControllers();
      var app = builder.Build();
      // Configure the HTTP request pipeline.
      app.UseHttpsRedirection();
      app.UseAuthorization();
      app.MapControllers();
      app.Run();
      

      复制 产品 模型

      Visual Studio Visual Studio Code
    13. 在“解决方案资源管理器”中,右键单击项目。 选择“添加”>“新建文件夹”。 将文件夹命名为“Models”。
    14. 右键单击“Models”文件夹。 选择“添加”>“类” 。 将类命名为 Product ,然后选择“ 添加”。
    15. 将模板模型代码替换为以下内容:
    16. public int Id { get; set; } public string? Name { get; set; } public string? Category { get; set; } public decimal Price { get; set; }

      前面突出显示的代码会更改以下内容:

    17. ?添加了 批注,以将 NameCategory 属性声明为可为 null 的引用类型。
    18. 利用 C# 8 中引入的可为 Null 功能,ASP.NET Core可以在处理引用类型时提供额外的代码流分析和编译时安全性。 例如,防止 null 引用异常。

      在本例中,意图是 NameCategory 可以是可以为 null 的类型。

      ASP.NET Core 6.0 项目默认启用可为空引用类型。 有关详细信息,请参阅可为空引用类型

      复制 ProductsController

      Visual Studio Visual Studio Code
    19. 选择 “添加 > 控制器...”
    20. “添加新基架项 ”对话框中,选择“ Mvc 控制器 - 空 ”,然后选择“ 添加”。
    21. 将控制器命名 为 ProductsController ,然后选择“ 添加”。
    22. 将模板控制器代码替换为以下内容:
    23. [Route("api/[controller]")] [ApiController] public class ProductsController : ControllerBase Product[] products = new Product[] new Product Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 new Product Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M new Product Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M [HttpGet] public IEnumerable<Product> GetAllProducts() return products; [HttpGet("{id}")] public ActionResult<Product> GetProduct(int id) var product = products.FirstOrDefault((p) => p.Id == id); if (product == null) return NotFound(); return product;

      前面突出显示的代码会更改以下内容,以迁移到 ASP.NET Core:

    24. 删除 ASP.NET Core中不存在的以下 ASP.NET 4.x 组件的 using 语句:

    25. ApiController
    26. System.Web.Http 命名空间
    27. IHttpActionResult 接口
    28. using ProductsApp.Models; 语句更改为 using ProductsCore.Models;

    29. 将根命名空间设置为 ProductsCore

    30. ApiController 更改为 ControllerBase

    31. 添加 using Microsoft.AspNetCore.Mvc; 以解析 ControllerBase 引用。

    32. GetProduct 操作的返回类型从 IHttpActionResult 更改为 ActionResult<Product>。 有关详细信息,请参阅 控制器操作返回类型

    33. GetProduct 操作的 return 语句简化为以下语句:

      return product;
      
    34. 添加以下属性,这些属性将在后续部分中介绍:

    35. [Route("api/[controller]")]
    36. [ApiController]
    37. [HttpGet]
    38. [HttpGet("{id}")]
    39. ASP.NET Core提供了一个最小托管模型,其中终结点路由中间件包装整个中间件管道,因此可以直接将路由添加到 ,WebApplication而无需显式调用UseEndpointsUseRouting注册路由。

      UseRouting 仍可用于指定进行路由匹配的位置,但如果应在中间件管道开头匹配路由,则无需显式调用 UseRouting

      var builder = WebApplication.CreateBuilder(args);
      // Add services to the container.
      builder.Services.AddControllers();
      var app = builder.Build();
      // Configure the HTTP request pipeline.
      app.UseHttpsRedirection();
      app.UseAuthorization();
      app.MapControllers();
      app.Run();
      

      注意:直接添加到 WebApplication 的路由在管道的末端执行。

      迁移的 中的路由 ProductsController

      迁移的 ProductsController 包含以下突出显示的属性:

      using Microsoft.AspNetCore.Mvc;
      using ProductsCore.Models;
      namespace ProductsCore.Controllers;
      [Route("api/[controller]")]
      [ApiController]
      public class ProductsController : ControllerBase
          Product[] products = new Product[]
                  new Product
                      Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1
                  new Product
                      Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M
                  new Product
                      Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M
          [HttpGet]
          public IEnumerable<Product> GetAllProducts()
              return products;
          [HttpGet("{id}")]
          public ActionResult<Product> GetProduct(int id)
              var product = products.FirstOrDefault((p) => p.Id == id);
              if (product == null)
                  return NotFound();
              return product;
      
    40. 属性[Route]配置控制器的属性路由模式。

    41. [ApiController] 属性使得此控制器中所有操作都必须使用属性路由。

    42. 属性路由支持令牌,例如 [controller][action]。 在运行时,每个标记分别替换为应用了属性的控制器或操作的名称。 令牌执行以下操作:

    43. 减少或消除对路由使用硬编码字符串的需要。
    44. 应用自动重命名重构时,请确保路由与相应的控制器和操作保持同步。
    45. 为具有以下属性的操作启用 ProductController HTTP Get 请求:

    46. [HttpGet] 应用于操作的属性 GetAllProducts
    47. [HttpGet("{id}")] 应用于操作的属性 GetProduct
    48. 运行迁移的项目,并浏览到 /api/products。 例如: https://localhost:<port>/api/products。 这会显示包含三个产品的完整列表。 浏览到 /api/products/1。 此时显示第一个产品。

      查看或下载示例代码如何下载

    49. 使用 ASP.NET Core 创建 Web API
    50. ASP.NET Core Web API 中控制器操作的返回类型
    51. ASP.NET Core MVC 的兼容性版本
    52. 本文演示从 ASP.NET 4.x Web API 迁移到 ASP.NET Core MVC 所需的步骤。

      查看或下载示例代码如何下载

    53. 具有“ASP.NET 和 Web 开发”工作负载的 Visual Studio 2019 16.4 或更高版本

    54. .NET Core 3.1 SDK

      查看 ASP.NET 4.x Web API 项目

      本文使用在 ASP.NET Web API 2 入门中创建的 ProductsApp 项目。 在该项目中,按如下方式配置基本 ASP.NET 4.x Web API 项目。

      在 中Global.asax.cs,调用 :WebApiConfig.Register

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Http;
      using System.Web.Routing;
      namespace ProductsApp
          public class WebApiApplication : System.Web.HttpApplication
              protected void Application_Start()
                  GlobalConfiguration.Configure(WebApiConfig.Register);
      

      WebApiConfig 位于 App_Start 文件夹中,具有静态 Register 方法:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web.Http;
      namespace ProductsApp
          public static class WebApiConfig
              public static void Register(HttpConfiguration config)
                  // Web API configuration and services
                  // Web API routes
                  config.MapHttpAttributeRoutes();
                  config.Routes.MapHttpRoute(
                      name: "DefaultApi",
                      routeTemplate: "api/{controller}/{id}",
                      defaults: new { id = RouteParameter.Optional }
      

      上述类会执行以下操作:

    55. 配置属性路由(虽然实际上并没有使用它)。
    56. 配置路由表。 示例代码要求 URL 匹配格式 /api/{controller}/{id},其中 {id} 是可选的。
    57. 以下部分演示了如何将 Web API 项目迁移到 ASP.NET Core MVC。

      创建目标项目

      在 Visual Studio 中创建新的空白解决方案,并添加 ASP.NET 4.x Web API 项目进行迁移:

    58. 从“文件”菜单中选择“新建”>“项目” 。
    59. 选择“空白解决方案”模板,然后选择“下一步”。
    60. 将解决方案命名为 WebAPIMigration。 选择“创建”。
    61. 将现有的 ProductsApp 项目添加到解决方案。
    62. 添加要迁移到的新 API 项目:

    63. 将新的“ASP.NET Core Web 应用程序”项目添加到解决方案中。
    64. 在“配置新项目”对话框中,将项目命名为 ProductsCore,然后选择“创建”。
    65. 在“创建新的 ASP.NET Core Web 应用程序”对话框中,确认选择“.NET Core”和“ASP.NET Core 3.1” 。 选择“API”项目模板,然后选择“创建” 。
    66. WeatherForecast.cs从新的 ProductsCore 项目中删除 和 Controllers/WeatherForecastController.cs 示例文件。
    67. 解决方案包含两个项目。 以下部分介绍了如何将 ProductsApp 项目的内容迁移到 ProductsCore 项目。

      ASP.NET Core 不使用 App_Start 文件夹或 Global.asax 文件。 此外,还会在发布时添加 web.config 文件。

      Startup 类:

    68. 替换 Global.asax。
    69. 处理所有应用启动任务。
    70. 有关详细信息,请参阅 ASP.NET Core 中的应用启动

      迁移模型和控制器

      下面的代码演示了要为 ASP.NET Core 更新的 ProductsController

      using ProductsApp.Models;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Net;
      using System.Web.Http;
      namespace ProductsApp.Controllers
          public class ProductsController : ApiController
              Product[] products = new Product[] 
                  new Product
                      Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1
                  new Product
                      Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M
                  new Product
                      Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M
              public IEnumerable<Product> GetAllProducts()
                  return products;
              public IHttpActionResult GetProduct(int id)
                  var product = products.FirstOrDefault((p) => p.Id == id);
                  if (product == null)
                      return NotFound();
                  return Ok(product);
      

      更新 ASP.NET Core 的 ProductsController

    71. 模型文件夹从原始项目复制到Controllers/ProductsController.cs新项目。
    72. 将复制的文件的根命名空间更改为 ProductsCore
    73. using ProductsApp.Models; 语句更新为 using ProductsCore.Models;
    74. ASP.NET Core 中不存在下列组件:

    75. ApiController
    76. System.Web.Http 命名空间
    77. IHttpActionResult 接口
    78. 进行以下更改:

    79. ApiController 更改为 ControllerBase。 添加 using Microsoft.AspNetCore.Mvc; 来解析 ControllerBase 引用。

    80. 删除 using System.Web.Http;

    81. GetProduct 操作的返回类型从 IHttpActionResult 更改为 ActionResult<Product>

    82. GetProduct 操作的 return 语句简化为如下内容:

      return product;
      

      ASP.NET Core API 项目模板在生成的代码中包含终结点路由配置。

      以下 UseRoutingUseEndpoints 调用会执行以下操作:

    83. 中间件管道中注册路由匹配和终结点执行。
    84. 替换 ProductsApp 项目的 App_Start/WebApiConfig.cs 文件。
    85. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          if (env.IsDevelopment())
              app.UseDeveloperExceptionPage();
          app.UseHttpsRedirection();
          app.UseRouting();
          app.UseAuthorization();
          app.UseEndpoints(endpoints =>
              endpoints.MapControllers();
      

      按如下所示配置路由:

    86. 使用以下属性标记 ProductsController 类:

      [Route("api/[controller]")]
      [ApiController]
      

      上述 [Route] 属性会配置控制器的属性路由模式。 [ApiController] 属性使得此控制器中所有操作都必须使用属性路由。

      属性路由支持标记,例如 [controller][action]。 在运行时,每个标记分别替换为应用了属性的控制器或操作的名称。 令牌执行以下操作:

    87. 减少项目中的魔幻字符串的数量。
    88. 应用自动重命名重构时,请确保路由与相应的控制器和操作保持同步。
    89. 启用对 ProductController 操作的 HTTP Get 请求:

    90. [HttpGet] 属性应用于 GetAllProducts 操作。
    91. [HttpGet("{id}")] 属性应用于 GetProduct 操作。
    92. 运行迁移的项目,并浏览到 /api/products。 这会显示包含三个产品的完整列表。 浏览到 /api/products/1。 此时显示第一个产品。

    93. 使用 ASP.NET Core 创建 Web API
    94. ASP.NET Core Web API 中控制器操作的返回类型
    95. ASP.NET Core MVC 的兼容性版本
  •