在 WEB 中通过方法重载传递多个参数 API

multiple parameters passing with method overloading in WEB API

我在存储库中有以下方法Class

    public class LibraryRepository : IBookRepository
    {
        LibraryContext context = new LibraryContext();

        public decimal findBookPrice(int book_id)
        {
            var bookprice = (
                            from r in context.Books
                            where r.Book_Id == book_id
                            select r.Price
                            ).FirstOrDefault();

            return bookprice;

        }

        public decimal findBookPrice(int book_id, string bookname)
        {
            var bookprice = (
                             from book in context.Books
                             where book.Book_Id == book_id & book.Book_Title == bookname
                             select book.Price
                             ).FirstOrDefault();

            return bookprice;

        }  

    }

然后我尝试在 Web 中分别获取这两种方法 API

    public class BooksWithAuthersController : ApiController
    {

        private LibraryRepository db = new LibraryRepository();

        // GET: api/BooksWithAuthers/id/Price  

        [ResponseType(typeof(decimal))]
        [Route("api/BooksWithAuthers/{id}/Price")]
        public IHttpActionResult GetBooksPriceById(int id)
        {
            decimal bookprice = db.findBookPrice(id);

            return Ok(bookprice);
        }

        // GET: api/BooksWithAuthers/id,name/Price

        [ResponseType(typeof(decimal))]
        [Route("api/BooksWithAuthers/{id,name}/Price")]
        public IHttpActionResult GetBooksPriceById(int id,string name)
        {
            decimal bookprice = db.findBookPrice(id,name);

            return Ok(bookprice);
        }
    }

这里第一种方法工作正常,但我如何处理具有多个参数的场景

将您的参数设置为 nullable 类型,如下所示:

public class LibraryRepository : IBookRepository, I
    {
        LibraryContext context = new LibraryContext();

        public decimal findBookPrice(int book_id=0)
        {
            var bookprice = (
                            from r in context.Books
                            where r.Book_Id == book_id
                            select r.Price
                            ).FirstOrDefault();

            return bookprice;

        }

        public decimal findBookPrice(int book_id=0, string bookname=null)
        {
            var bookprice = (
                             from book in context.Books
                             where book.Book_Id == book_id & book.Book_Title == bookname
                             select book.Price
                             ).FirstOrDefault();

            return bookprice;

        }  

    }

并设置一个条件来检查 nullable 参数,如下所示:

if(book_id!=0)
{
    // do your logic here
}
if(!string.IsNullOrEmpty(bookname))
{
   // do your logic here
}

希望对您有所帮助

谢谢

我不确定这个 {id,name} 是否甚至 possible.You 正在获取异常,因为路由模块正在读取 5,作为单个属性进行测试并且无法将其序列化为 int 方法

public IHttpActionResult GetBooksPriceById(int id)

考虑到可为 null 的类型(int?),您需要如下更改方法并更改路由

[ResponseType(typeof(decimal))]
[Route("api/BooksWithAuthers/{id}/Price")]
public IHttpActionResult GetBooksPriceById(int? id)
{
   decimal bookprice = db.findBookPrice(id);
   return Ok(bookprice);
}


[ResponseType(typeof(decimal))]
[Route("api/BooksWithAuthers/{id}/{name}/Price")]
public IHttpActionResult GetBooksPriceById(int? id,string name = null)
{
    decimal bookprice = db.findBookPrice(id,name);

    return Ok(bookprice);
}

参考 - https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx