I am creating a Web API GET (all) method with multiple optional parameters. I'm trying this with 1 parameter first but eventually, I want 5 optional parameters. Starting with 1 parameter I have 2 situations: parameter is filled in and parameter is not filled in.
Parameter is filled in
from r in db.requests where r.status == status selectnewModels.Request
Parameter is not filled in
from r in db.requests selectnewModels.Request
I can not get both situations to work together so my question is: How can I combine these 2 situations?
Controller
publicIEnumerable<Request>Get(string status =""){var requests =from r in db.requests //where r.status == statusselectnewModels.Request{ ID = r.ID,...more properties };return(IEnumerable<Request>)requests;}
Route
protectedvoidApplication_Start(){RouteTable.Routes.MapHttpRoute( name:"API Default", routeTemplate:"api/{controller}/{id}", defaults:new{ id =RouteParameter.Optional});}
Anonymous User
04-Nov-2014Try the following code:
If the status is empty string second part of the expression is true and all items will be returned no matter the value of the first part. Otherwise, the first part of the expression filters the result set.