2016-09-16 61 views
1

我在倉庫類下面的方法多個參數與方法重載傳遞WEB API

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

+0

是逗號分隔的事{ID,名稱}甚至有可能?任何對此的引用? –

回答

0

我不知道這個{id,name}甚至可能。你會得到例外,因爲rou婷模塊正在讀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

+0

我試過你的方法,但這裏得到異常一旦id == null http://imgur.com/a/pTPLI – kez

+0

更新可空類型 –

+0

感謝它的成功:) – kez

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 
} 

希望這將幫助你

感謝