使用 ASP.NET Core MVC 创建 Web API(三)

原文:使用 ASP.NET Core MVC 创建 Web API(三)

使用 ASP.NET Core MVC 创建 Web API

使用 ASP.NET Core MVC 创建 Web API(一)

使用 ASP.NET Core MVC 创建 Web API(二)

十、添加 GetBookItem 方法

1) 在Visual Studio 2017中的“解决方案资源管理器”中双击打开BookController文件,添加Get方法的API。代码如下。

// GET: api/Book
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Book>>> GetBookItems()
        {
            return await _context.Book.ToListAsync();
        }

        // GET: api/Book/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Book>> GetBookItem(int id)
        {
            var bookItem = await _context.Book.FindAsync(id);

            if (bookItem == null)
            {
                return NotFound();
            }
            return bookItem;
        }
 2) 将上面的代码添加到BookController文件中后,会在ToListAsync方法下出现波浪线,    这时需要把using Microsoft.EntityFrameworkCore;添加到文件开头。
 

3) 这两个方法实现两个 GET 终结点:

  • GET /api/book
  • GET /api/book/{id}

4) 在Visual Studio 2017中按F5,运行应用程序。然后在浏览器中分别调用两个终结点来测试应用。

5) 在浏览器中输入“http://localhost:5000/api/book”查询所有书籍信息,如下图。

6) 在浏览器中输入“http://localhost:5000/api/book/8”查询指定有书籍信息,如下图。

十一、路由和 URL 路径

   [HttpGet] 属性表示响应 HTTP GET 请求的方法。 每个方法的 URL 路径构造如下所示:

  • 在控制器的 Route 属性中以模板字符串开头:
namespace BookApi.Controllers
{
 [Route("api/[controller]")]
    [ApiController]
    public class BookController : Controller
    {
        private readonly BookContext _context;
  • [controller] 替换为控制器的名称,按照惯例,在控制器类名称中去掉“Controller”后缀。 对于此示例,控制器类名称为“Book”控制器,因此控制器名称为“Book”。 ASP.NET Core 路由不区分大小写。
  • 如果 [HttpGet] 属性具有路由模板(例如 [HttpGet("Book")]),则将它追加到路径。 此示例不使用模板。

在下面的 GetBookItem 方法中,"{id}" 是书籍信息的唯一标识符的占位符变量。 调用 GetBookItem 时,URL 中 "{id}" 的值会在 id 参数中提供给方法。

   // GET: api/Book/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Book>> GetBookItem(int id)

        {
            var bookItem = await _context.Book.FindAsync(id);

            if (bookItem == null)
            {
                return NotFound();
            }
            return bookItem;
        }

十二、测试 GetBookItems 方法

本教程使用 Rester 测试 Web API。

1) 安装Firefox的组件Rester

2) 在Visual Studio 2017中启动 Web 应用程序。

3) 打开Rester。

4) 创建新请求,将 HTTP 方法设置为“GET”,将请求 URL 设置为 http://localhost:5000/api/Book/24。如下图。

5)  选择“Send”。  返回结果,如下图。

原文地址:https://www.cnblogs.com/lonelyxmas/p/10862002.html

时间: 2024-08-05 19:37:29

使用 ASP.NET Core MVC 创建 Web API(三)的相关文章

使用 ASP.NET Core MVC 创建 Web API(四)

原文:使用 ASP.NET Core MVC 创建 Web API(四) 使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 使用 ASP.NET Core MVC 创建 Web API(三) 十三.返回值 在上一篇文章(使用 ASP.NET Core MVC 创建 Web API(二))中我们创建了GetBookItems和 GetBookItem两

使用 ASP.NET Core MVC 创建 Web API(一)

原文:使用 ASP.NET Core MVC 创建 Web API(一) 从今天开始来学习如何在 ASP.NET Core 中构建 Web API 以及每项功能的最佳适用场景.关于此次示例的数据库创建请参考<学习ASP.NET Core Razor 编程系列一>    至  <学习ASP.NET Core Razor 编程系列十九——分页> 一.概述 本教程将创建以下 Web API: API 说明 请求正文 响应正文 GET /api/Book 获取所有的书籍信息 None 书籍

使用 ASP.NET Core MVC 创建 Web API(二)

原文:使用 ASP.NET Core MVC 创建 Web API(二) 使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framework Core功能的主类. 此类由 Microsoft.EntityFrameworkCore.DbContext 类派生而来. 1) 在Visual Studio 2017的“解决方案资源管理器”中,右键单击“Models”文

【转载】从头编写 asp.net core 2.0 web api 基础框架 (1)

工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相关知识就不介绍了, 这里主要是从头编写一个asp.net core 2.0 web api的基础框架. 我一直在关注asp.net core 和 angular 2/4, 并在用这对开发了一些比较小的项目. 现在我感觉是时候使用这两个技术去为企业开发大一点的项目了, 由于企业有时候需要SSO(单点登

从头编写 asp.net core 2.0 web api 基础框架 (1)

原文:从头编写 asp.net core 2.0 web api 基础框架 (1) 工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相关知识就不介绍了, 这里主要是从头编写一个asp.net core 2.0 web api的基础框架. 我一直在关注asp.net core 和 angular 2/4, 并在用这对开发了一些比较小的项目. 现在我感

【ASP.NET Core学习】Web API

这里介绍在ASP.NET Core中使用Web API创建 RESTful 服务,本文使用VSCode + NET Core3.0 创建简单Rest API 格式化输出 JSON Patch请求 Open API(Swagger)集成 创建简单Rest API 在终端输入 dotnet new webapi -n WebAPI 1. 创建Order模型,然后初始化数据 public class OrderStore { public List<Order> Orders { get; } =

从头编写 asp.net core 2.0 web api 基础框架 (4) EF配置

原文:从头编写 asp.net core 2.0 web api 基础框架 (4) EF配置 第1部分:http://www.cnblogs.com/cgzl/p/7637250.html 第2部分:http://www.cnblogs.com/cgzl/p/7640077.html 第3部分:http://www.cnblogs.com/cgzl/p/7652413.html Github源码地址:https://github.com/solenovex/Building-asp.net-co

从头编写 asp.net core 2.0 web api 基础框架 (5) EF CRUD

原文:从头编写 asp.net core 2.0 web api 基础框架 (5) EF CRUD 第1部分:http://www.cnblogs.com/cgzl/p/7637250.html 第2部分:http://www.cnblogs.com/cgzl/p/7640077.html 第3部分:http://www.cnblogs.com/cgzl/p/7652413.html 第4部分:http://www.cnblogs.com/cgzl/p/7661805.html Github源码

从头编写 asp.net core 2.0 web api 基础框架 (3)

原文:从头编写 asp.net core 2.0 web api 基础框架 (3) 第一部分:http://www.cnblogs.com/cgzl/p/7637250.html 第二部分:http://www.cnblogs.com/cgzl/p/7640077.html Github源码地址:https://github.com/solenovex/Building-asp.net-core-2-web-api-starter-template-from-scratch 之前我介绍完了asp