multiple parameters passing with method overloading in WEB API

ⅰ亾dé卋堺 提交于 2019-12-11 06:39:15

问题


I have following methods in 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;

        }  

    }

Then I'm trying to get those two methods separately in 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);
        }
    }

Here first method working fine, but How can I handle scenario with multiple parameters


回答1:


I am not sure if this {id,name} is even possible.You are getting the exception because routing module is reading 5,test as single attribute and is not able serialize it to int for method

public IHttpActionResult GetBooksPriceById(int id)

You need to change your methods as below considering nullable types (int?) and also change the route

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

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




回答2:


Set your parameter as a nullable type like below:

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;

        }  

    }

and set a condition simply to check nullable parameter like below:

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

Hope this will help you

Thanks



来源:https://stackoverflow.com/questions/39526334/multiple-parameters-passing-with-method-overloading-in-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!