四时宝库

程序员的知识宝库

从 ASP.NET Web API 迁移到 ASP.NET Core

ASP.NET 的 4.x Web API 是一种 HTTP 服务,它可达到各种客户端,包括浏览器和移动设备。 ASP.NET Core 将 ASP.NET 4.x 的 MVC 和 Web API 应用模型组合到称为 ASP.NET Core MVC 的单一编程模型中。 本文演示从 ASP.NET 4.x Web API 迁移到 ASP.NET Core MVC 所需的步骤。

先决条件

  • 具有“ASP.NET 和 Web 开发”工作负载的 Visual Studio 2019 16.4 或更高版本
  • .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 执行以下操作:

C#

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 方法:

C#

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 }
            );
        }
    }
}

前面的类:

  • 配置 属性路由,但实际上并没有使用它。
  • 配置路由表。 示例代码需要 Url 来匹配格式,这 /api/{controller}/{id} {id} 是可选的。

以下部分演示了如何将 Web API 项目迁移到 ASP.NET Core MVC。

创建目标项目

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

  1. 从“文件”菜单中选择“新建”>“项目” 。
  2. 选择 " 空白解决方案 " 模板,然后选择 " 下一步"。
  3. 将解决方案命名为 WebAPIMigration。 选择“创建”。
  4. 将现有的 ProductsApp 项目添加到解决方案。

添加要迁移到的新 API 项目:

  1. 向解决方案添加新的 ASP.NET Core Web 应用程序 项目。
  2. 在 " 配置新项目 " 对话框中,将项目命名为 ProductsCore,然后选择 " 创建"。
  3. 在“创建新的 ASP.NET Core Web 应用程序”对话框中,确认选择“.NET Core”和“ASP.NET Core 3.1” 。 选择“API”项目模板,然后选择“创建” 。
  4. 从新的 ProductsCore 项目中删除 WeatherForecast 和 controller /WeatherForecastController 示例文件。

解决方案现在包含两个项目。 以下各节介绍了如何将 ProductsApp 项目的内容迁移到 ProductsCore 项目。

迁移配置

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

Startup 类:

  • 替换 global.asax
  • 处理所有应用启动任务。

有关详细信息,请参阅 ASP.NET Core 中的应用启动。

迁移模型和控制器

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

C#

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);
        }
    }
}

更新 ProductsController ASP.NET Core 的:

  1. 控制器/ProductsController模型 文件夹从原始项目复制到新项目。
  2. 将复制的文件的根命名空间更改为 ProductsCore 。
  3. 将 using ProductsApp.Models; 语句更新到 using ProductsCore.Models; 。

ASP.NET Core 中不存在下列组件:

  • ApiController 类
  • System.Web.Http 命名空间
  • IHttpActionResult 接口

进行以下更改:

  1. 将 ApiController 更改为 ControllerBase。 添加 using Microsoft.AspNetCore.Mvc; 以解析 ControllerBase 引用。
  2. 删除 using System.Web.Http;。
  3. 将 GetProduct 操作的返回类型从更改 IHttpActionResult 为 ActionResult<Product> 。
  4. 简化 GetProduct 操作的 return 语句:C#复制return product;

配置路由

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

以下 UseRouting 和 UseEndpoints 调用:

  • 在 中间件 管道中注册路由匹配和终结点执行。
  • 替换 ProductsApp 项目的 App_Start/webapiconfig.cs 文件。

C#

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

按如下所示配置路由:

  1. ProductsController用以下特性标记类:C#复制[Route("api/[controller]")] [ApiController] 上述 [Route] 属性配置控制器的属性路由模式。 [ApiController]特性使特性路由成为此控制器中所有操作的要求。特性路由支持标记,例如 [controller] 和 [action] 。 在运行时,每个标记分别替换为应用了属性的控制器或操作的名称。 令牌:减少项目中的幻数字符串的数目。应用自动重命名重构时,请确保路由与相应的控制器和操作保持同步。
  2. 为操作启用 HTTP Get 请求 ProductController :将特性应用于 [HttpGet] GetAllProducts 操作。将特性应用于 [HttpGet("{id}")] GetProduct 操作。

运行迁移的项目,并浏览到 /api/products 。 此时会显示三个产品的完整列表。 浏览到 /api/products/1。 第一个产品随即出现。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接