WEB API2支持新的屬性路由,本章會使用到一些實際項目中,此API會支持以下的一些Action
Paste_Image.png
所有方法都是只讀的HTTP請求
我們會使用Entity Framework,圖書記錄會包含以下字段
ID
Title
Genre
Publication date
Price
Description
AuthorID (foreign key to an Authors table)
首先創建一個WEB API空項目,此過程不做詳細說明。
Paste_Image.png
然后右鍵項目文件夾 Models添加類 名稱Author
Paste_Image.png
添加另外一個類,名稱Book
Paste_Image.png
添加控制器 右鍵Controllers 文件夾,添加控制器
Paste_Image.png
在添加窗口中 名稱是Books 模型類為Book,勾選使用異步控制器操作
Paste_Image.png
新建上下文 添加 確定
Paste_Image.png
會添加成功2個文件
Paste_Image.png
創建數據庫 選擇 工具欄 NuGet包管理器 程序包管理控制臺
輸入 enable-migrations
Paste_Image.png
生成了新的文件
Paste_Image.png
打開此文件,替換以下代碼。
protected override void Seed(BooksAPI.Models.BooksAPIContext context)
{
context.Authors.AddOrUpdate(new Author[] {
new Author() { AuthorId = 1, Name = "Ralls, Kim" },
new Author() { AuthorId = 2, Name = "Corets, Eva" },
new Author() { AuthorId = 3, Name = "Randall, Cynthia" },
new Author() { AuthorId = 4, Name = "Thurman, Paula" }
});
context.Books.AddOrUpdate(new Book[] {
new Book() { BookId = 1, Title= "Midnight Rain", Genre = "Fantasy",
PublishDate = new DateTime(2000, 12, 16), AuthorId = 1, Description =
"A former architect battles an evil sorceress.", Price = 14.95M },
new Book() { BookId = 2, Title = "Maeve Ascendant", Genre = "Fantasy",
PublishDate = new DateTime(2000, 11, 17), AuthorId = 2, Description =
"After the collapse of a nanotechnology society, the young" +
"survivors lay the foundation for a new society.", Price = 12.95M },
new Book() { BookId = 3, Title = "The Sundered Grail", Genre = "Fantasy",
PublishDate = new DateTime(2001, 09, 10), AuthorId = 2, Description =
"The two daughters of Maeve battle for control of England.", Price = 12.95M },
new Book() { BookId = 4, Title = "Lover Birds", Genre = "Romance",
PublishDate = new DateTime(2000, 09, 02), AuthorId = 3, Description =
"When Carla meets Paul at an ornithology conference, tempers fly.", Price = 7.99M },
new Book() { BookId = 5, Title = "Splish Splash", Genre = "Romance",
PublishDate = new DateTime(2000, 11, 02), AuthorId = 4, Description =
"A deep sea diver finds true love 20,000 leagues beneath the sea.", Price = 6.99M},
});
}
在程序包管理器控制臺窗口中,鍵入以下命令
add-migration Initial
update-database
PM> add-migration Initial
Scaffolding migration 'Initial'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration Initial' again.
PM> Add-Migration Initial
Re-scaffolding migration 'Initial'.
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201701170713346_Initial].
Applying explicit migration: 201701170713346_Initial.
Running Seed method.
這些命令可以創建一個本地數據庫,并且運行Seed方法填充數據。
添加DTO類 解決方案中添加新文件夾,命名為Dto,然后添加BookDto到該文件夾下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BooksAPI.Dto
{
public class BookDto
{
public string Title { get; set; }
public string Author { get; set; }
public string Genre { get; set; }
}
}
添加另一個名為BookDetailDto的類.
using System;
namespace BooksAPI.DTOs
{
public class BookDetailDto
{
public string Title { get; set; }
public string Genre { get; set; }
public DateTime PublishDate { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Author { get; set; }
}
}
以下是BookCtrl中的代碼
using BooksAPI.Dto;
using BooksAPI.DTOs;
using BooksAPI.Models;
using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
namespace BooksAPI.Controllers
{
public class BooksController : ApiController
{
private BooksAPIContext db = new BooksAPIContext();
// Typed lambda expression for Select() method.
private static readonly Expression<Func<Book, BookDto>> AsBookDto =
x => new BookDto
{
Title = x.Title,
Author = x.Author.Name,
Genre = x.Genre
};
// GET api/Books
public IQueryable<BookDto> GetBooks()
{
return db.Books.Include(b => b.Author).Select(AsBookDto);
}
// GET api/Books/5
[ResponseType(typeof(BookDto))]
public async Task<IHttpActionResult> GetBook(int id)
{
BookDto book = await db.Books.Include(b => b.Author)
.Where(b => b.BookId == id)
.Select(AsBookDto)
.FirstOrDefaultAsync();
if (book == null)
{
return NotFound();
}
return Ok(book);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
下來添加屬性路由
首先給控制器添加RoutePrefix屬性
Paste_Image.png
添加屬性路由
Paste_Image.png
獲取書籍詳細信息
[Route("{id:int}/Details")]
[ResponseType(typeof(BookDetailDto))]
public async Task<IHttpActionResult> GetBookDetail(int id)
{
var book = await (from b in db.Books.Include(b => b.Author)
where b.AuthorId == id
select new BookDetailDto
{
Title = b.Title,
Genre = b.Genre,
PublishDate = b.PublishDate,
Price = b.Price,
Description = b.Description,
Author = b.Author.Name
}).FirstOrDefaultAsync();
if (book == null)
{
return NotFound();
} else
{
return Ok(book);
}
}
獲取指定分類的書籍
[Route("{genre}")]
public IQueryable<BookDto> GetBookByGenre(string genre)
{
return db.Books.Include(b => b.Author)
.Where(b => b.Genre.Equals(genre, StringComparison.OrdinalIgnoreCase))
.Select(AsBookDto);
}
獲取指定作者的書籍列表
[Route("~/api/authors/{authorId}/books")]
public IQueryable<BookDto> GetBooksByAuthor(int authorId)
{
return db.Books.Include(b => b.Author)
.Where(b => b.AuthorId == authorId)
.Select(AsBookDto);
}
按出版日期獲取書籍
//此處使用正則表達式來匹配日期格式,并且匹配多個格式的時間
[Route("date/{pubdate:datetime:regex(\\d{4}-\\d{2}-\\d{2})}")]
[Route("date/{*pubdate:datetime:regex(\\d{4}/\\d{2}/\\d{2})}")]
public IQueryable<BookDto> GetBooks(DateTime pubdate)
{
return db.Books.Include(b => b.Author)
.Where(b => DbFunctions.TruncateTime(b.PublishDate)
== DbFunctions.TruncateTime(pubdate))
.Select(AsBookDto);
}
分別請求測試
獲取所有書籍
Paste_Image.png
獲取指定書籍
Paste_Image.png
獲取指定書籍的詳細信息
Paste_Image.png
根據分類獲取
Paste_Image.png
根據作者ID獲取書籍列表
Paste_Image.png
根據出版日期獲取書籍列表
Paste_Image.png