使用 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/bookGET /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”。 返回结果,如下图。

使用 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(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...
 - 使用 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(二) 使 ...
 - 使用 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 中构建 Web API 以及每项功能的最佳适用场景.关于此次示例的数据库创建请参考<学习ASP.NET Core Razor 编程系列一> ...
 - 使用.Net Core MVC创建Web API
		
创建.Net Core MVC 打开appsettings.json文件,添加数据库连接 { "Logging": { "LogLevel": { " ...
 - 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】
		
Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...
 - 为什么 web 开发人员需要迁移到. NET Core, 并使用 ASP.NET Core MVC 构建 web 和 webservice/API
		
2018 .NET开发者调查报告: .NET Core 是怎么样的状态,这里我们看到了还有非常多的.net开发人员还在观望,本文给大家一个建议.这仅代表我的个人意见, 我有充分的理由推荐.net 程序 ...
 
随机推荐
- get.go
			
//获取空间文件 )) resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err ...
 - compact_op.go
			
package clientv3 import ( pb "github.com/coreos/etcd/etcdserver/etcdserverpb" ) // Com ...
 - BZOJ_3670_[Noi2014]动物园_KMP
			
BZOJ_3670_[Noi2014]动物园_KMP Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的 ...
 - Java IO--字符流--InputStreamReader 和 OutputStreamWriter
			
今天继续学习字符流的子类!!!! 先来熟悉一下适配器设计模式:(手写的,,嘿嘿) 因为据说InputStreamReader 和OutputStreamWriter采用了适配器模式(现在我还没能理解, ...
 - 下拉框选择selected
			
var id=$("#orderId").children('option:selected').val(); //selected选中项取值 var id=$('#orderId ...
 - SPPNET
			
SPPNet Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 文章地址:https://ar ...
 - 阿里开源分布式事务解决方案  Fescar
			
微服务倡导将复杂的单体应用拆分为若干个功能简单.松耦合的服务,这样可以降低开发难度.增强扩展性.便于敏捷开发.当前被越来越多的开发者推崇,系统微服务化后,一个看似简单的功能,内部可能需要调用多个服务并 ...
 - python中字符串拆分与合并——split()、join()、strip()和replace()
			
Python3 split()方法 描述split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 语法split()方法语法: str.split(str= ...
 - 关于 JavaScript 中的继承
			
ES5 之前,继续是这样实现的 function Parent() { this.foo = function() { console.log('foo'); }; } Parent.prototyp ...
 - Git常用命令及软件推荐
			
查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id gi ...
 
			
		