如何从ASP.NET核心执行[HttpGet]以获取任意参数

时间:2019-06-18 13:15:48

标签: angular visual-studio asp.net-core http-get

我使用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]

2 个答案:

答案 0 :(得分:1)

在您的控制器中添加另一个操作,该操作接受字符串作为参数:

[HttpGet]
public IActionResult GetBookItemsByTitle(string title)
{
}

如果您要调用此操作,则网址为:

 const url = `${this.BookItemsUrl}?title=${title}`;

我建议看一下Routing in ASP.NET Core

编辑:

如果要重用当前存在的代码,请从路由中删除{id}并添加标题:

[HttpGet]
public IActionResult GetBookItems(string id, string title)
{
}

之后,您可以使用这些URL调用方法:

const url = `${this.BookItemsUrl}?id=${id}`;
const url = `${this.BookItemsUrl}?title=${title}`;
const url = `${this.BookItemsUrl}?id=${id}&title=${title}`;

答案 1 :(得分:1)

您可以通过定义路线约束来尝试此操作,例如

int限制,因此您的路线仅接受int id

[HttpGet("{id:int}")]
public IActionResult GetBookItems(int id)

字母站立(仅接受字母)

[HttpGet("{title:alpha}")]
public GetBookByTitle(string title)