错误的方法匹配时我的ASP.NET Core Crud调用有什么问题 [英] Whats wrong with my ASP.NET Core Crud call when wrong method match

查看:62
本文介绍了错误的方法匹配时我的ASP.NET Core Crud调用有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular ASP.NET核心控制器中有这三种方法,但是最后一个不触发,其他方法都可以,这是 GetBookItemsByTitle .自从我了解了这一点之后,我在这里的错误可能很明显.

I have this three methods in my Angular ASP.NET core Controller but the last one does not fire, the others works ok, it´s the GetBookItemsByTitle. Since I´m learning this my mistake here is probably obvious..

/// <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);
}

[HttpGet("{title}")]
public IActionResult GetBookItemsByTitle(string title)
{
    // some code
    return Ok(bookitems);
}

我称它为GetBookItemsByTitle(string title),甚至可以用-[HttpGet("{title}")]之类的"title"修饰它.

I call this GetBookItemsByTitle(string title), and I even decorate it with or without the "title" like - [HttpGet("{title}")].

我在日志中阅读:

请求启动HTTP/1.1 GET localhost:44378/api/BooksXml?title = aaaaa与{action ="GetBookItems",controller ="BooksXml",page ="}.

Request starting HTTP/1.1 GET localhost:44378/api/BooksXml?title=aaaaa Route matched with {action = "GetBookItems", controller = "BooksXml", page = ""}.

但是很明显,它使用了错误的方法.

but obviously it goes to wrong method.

在Angular客户端应用中,服务对应的三种方法是

In Angular client App the service corresponding three methods are

 /** 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=${title}`;
    return this.http.get<BookItem[]>(url);
  }

我的getBookByTitle Crud调用有什么问题?

Whats wrong with my getBookByTitle Crud call?

推荐答案

在Angular中,getBookByTitle的url错误,请参见您的getBookItem函数.网址应如下所示.

In Angular in getBookByTitle url is wrong see your getBookItem function. url should be like as follows.

getBookByTitle(title: string): Observable<BookItem[]> {
    const url = `${this.BookItemsUrl}/title/${title}`;
    return this.http.get<BookItem[]>(url);
  }

更改Api

[HttpGet("title/{title}")]
public IActionResult GetBookItemsByTitle(string title)
{
    // some code
    return Ok(bookitems);
}

这篇关于错误的方法匹配时我的ASP.NET Core Crud调用有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆