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

时间:2016-09-16 07:50:58

标签: c# asp.net-web-api overloading

我在Repository 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);
        }
    }

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

enter image description here

2 个答案:

答案 0 :(得分:0)

将参数设置为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
}

希望这会对你有所帮助

由于

答案 1 :(得分:0)

我不确定这个{id,name}是否可行。你得到的例外是因为路由模块正在读取5,测试为单个属性,并且无法将其序列化为int方法

public IHttpActionResult GetBooksPriceById(int id)

您需要在考虑可空类型(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