Reputation: 973
I want to know how can I pass multiple querystring values to my web api project. I made one by referring tutorial in which I can pass the ID.
// GET: api/Product
public IQueryable<product> Getproducts()
{
return db.products;
}
// GET: api/Product/5
[ResponseType(typeof(product))]
public IHttpActionResult Getproduct(int id)
{
product product = db.products.Find(id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
Please help me to understand what if I have multiple parameters to pass & how can I do it with querystring
UPDATE
public class ProductController : ApiController
{
public IEnumerable<product> Get()
{
using(foxbop7g_foxbox_mainEntities entities = new foxbop7g_foxbox_mainEntities())
{
return entities.products.ToList();
}
}
public product Get(string status)
{
using (foxbop7g_foxbox_mainEntities entities = new foxbop7g_foxbox_mainEntities())
{
return entities.products.FirstOrDefault(e => e.status == status);
}
}
}
Upvotes: 0
Views: 2977
Reputation: 147
You can accomplish that in one of the following ways:
Uri binding (Using [FromUri])
: You can pass the parameters in the url and then decorate the action parameter with [FromUri] attribute. for example: your method could be something like
public HttpResponseMessage Get([FromUri]String someInput1, [FromUri]String someInput2) {}
And you request uri can be like ...?someInput1=param1&someInput2=param2
from Request.GetQueryNameValuePairs(): this will get you all the query parameter as an eumerable key-value pair. Then you can do something like:
var queryStrings = Request.GetQueryNameValuePairs(); var key = "queryKey";//forexample:"someInput1" in the above url var match = queryStrings.FirstOrDefault(keyValue =>String.Compare(keyValue.Key,key,StringComparison.OrdinalIgnoreCase)==0); var value = match.Value;
Check these links for further understanding:
Upvotes: 0
Reputation: 1
If you search for specific record use Single or SingleOrDefault method with lambda expression (p => p.id). By SingleorDefault you execute query. Find is possible onnly when lazy loading is enabled. If you search for many use Where(also with lambda expression) and don't forget to add .ToList(); in the end as you want to iterate them to display for example. If there are foreign keys use .Include for eager loading.
Upvotes: 0
Reputation: 53958
Please help me to understand what if I have multiple parameters to pass & how can I do it with querystring
You could just add them as parameters to your method. For instance if there was a meaning to get a product by specifying product id and category you could define this:
[ResponseType(typeof(product))]
public IHttpActionResult Getproduct(int id, string category)
This is the common solution if you have to do this for a GET request. (GET request has not a body as POST or PUT etc. requests). If you have a POST, PUT, ...request, you could match your values by defining a model (a class with the properties you want to receive from the POST,PUT...), then define this as the only parameter of the method and prefix it with [FromBody]
attribute.
E.g.
[HttpPut]
public IHttpActionResult Customer([FromBody] CustomerModel)
and
public class CustomerModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
Upvotes: 2