0

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

3 Answers 3

2

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Followed your instructions. In my table there is a column 'status' Which has value active or inactive. I passed status as string & checked with this url localhost:56721/api/product/active . But it gives mt all products i.e. active & inactive. Is it because similarity with term 'active'?. Please check my updated code in post
What is the object that is returned ?
0

You can accomplish that in one of the following ways:

  1. 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

  2. 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:

  1. https://www.strathweb.com/2013/04/asp-net-web-api-parameter-binding-part-1-understanding-binding-from-uri/

  2. https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.