2016-09-16 65 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 
} 

希望这将帮助你

感谢