【发布时间】:2019-11-01 03:42:19
【问题描述】:
我通过 APS.NET 核心和 CRUD 原理学习 Angular
我有这两种方法:
/// <summary>
/// Retrieve all items from Books.
/// </summary>
/// <returns>Books items List</returns>
// GET: api/BooksXml
[HttpGet]
public IActionResult GetBookItems()
{
List<BookItem> BookItems = new List<BookItem>();
XDocument doc = _db.GetXmlDb();
List<BookItem> bookitems = doc.Descendants("book").Select(x => new BookItem()
{
Id = (string)x.Attribute("id"),
Author = (string)x.Element("author"),
Title = (string)x.Element("title"),
Genre = (string)x.Element("genre"),
Price = (decimal)x.Element("price"),
Publish_date = (DateTime)x.Element("publish_date"),
Description = (string)x.Element("description")
}).ToList();
return Ok(bookitems);
}
/// <summary>
/// Returns a Book item matching the given id.
/// </summary>
/// <param name="id">Id of item to be retrieved</param>
/// <returns>Book item</returns>
// GET: api/BooksXml/5
[HttpGet("{id}")]
public IActionResult GetBookItems(string id)
{
XDocument doc = _db.GetXmlDb();
XElement result = doc.Descendants("book").FirstOrDefault(el => el.Attribute("id") != null &&
el.Attribute("id").Value == id);
List<BookItem> BookItems = new List<BookItem>();
if (result != null)
{
BookItem Book = new BookItem();
Book.Id = (string)result.Attribute("id");
Book.Author = (string)result.Element("author");
Book.Title = (string)result.Element("title");
Book.Genre = (string)result.Element("genre");
Book.Price = (decimal)result.Element("price");
Book.Publish_date = (DateTime)result.Element("publish_date");
Book.Description = (string)result.Element("description");
BookItems.Add(Book);
}
return Ok(BookItems);
}
它们都是正确的 get 方法,我更想要的是另一个带有路由的 get,这样我就可以在我的后端书籍数据库中搜索书名。
像这样:(BooksXmlController.cs)
/** GET all books from server. */
getBookItems(): Observable<BookItem[]> {
return this.http.get<BookItem[]>(this.BookItemsUrl);
}
/** GET book by id. */
getBookItem(id: string): Observable<BookItem[]> {
const url = `${this.BookItemsUrl}/${id}`;
return this.http.get<BookItem[]>(url);
}
/** GET book by title from server. */
getBookByTitle(title: string): Observable<BookItem> {
const url = `${this.BookItemsUrl}/${title}`;
return this.http.get<BookItem>(url);
}
注意getBookByTitle,如何将“标题”映射到 ASP.NET 核心后端 [HttpGet]
【问题讨论】:
标签: c# angular typescript asp.net-core asp.net-core-mvc