简体   繁体   English

如何在asp.net WEB API中传递多个querystring参数

[英]how to pass multiple querystring parameters in asp.net WEB API

I want to know how can I pass multiple querystring values to my web api project. 我想知道如何将多个querystring值传递给我的Web api项目。 I made one by referring tutorial in which I can pass the ID. 我通过参考其中可以传递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 请帮助我了解如果我要传递多个参数怎么办以及如何使用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);
            }
        }
    }

Please help me to understand what if I have multiple parameters to pass & how can I do it with querystring 请帮助我了解如果我要传递多个参数怎么办以及如何使用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: 例如,如果有通过指定产品ID和类别来获得产品的含义,则可以定义以下内容:

[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请求执行此操作,这是常见的解决方案。 (GET request has not a body as POST or PUT etc. requests). (GET请求没有POST或PUT等请求的主体)。 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. 如果您有POST,PUT,...请求,则可以通过定义模型(具有要从POST,PUT ...接收的属性的类)来匹配值,然后将其定义为方法,并以[FromBody]属性为前缀。

Eg 例如

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

If you search for specific record use Single or SingleOrDefault method with lambda expression (p => p.id). 如果要搜索特定记录,请使用带有lambda表达式的Single或SingleOrDefault方法(p => p.id)。 By SingleorDefault you execute query. 通过SingleorDefault,您可以执行查询。 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(); 如果您搜索很多,请使用Where(也可以使用lambda表达式),并且不要忘记添加.ToList(); in the end as you want to iterate them to display for example. 最后,例如要迭代它们以进行显示。 If there are foreign keys use .Include for eager loading. 如果有外键,请使用.include进行紧急加载。

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. Uri binding (Using [FromUri]) :您可以在url中传递参数,然后使用[FromUri]属性修饰action参数。 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 例如:您的方法可能类似于public HttpResponseMessage Get([FromUri]String someInput1, [FromUri]String someInput2) {}而您请求的uri可能类似于...?someInput1=param1&someInput2=param2

  2. from Request.GetQueryNameValuePairs(): this will get you all the query parameter as an eumerable key-value pair. 从Request.GetQueryNameValuePairs()中获取:这将为您获得所有查询参数,作为一个易于理解的键值对。 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/ https://www.strathweb.com/2013/04/asp-net-web-api-parameter-binding-part-1-understanding-binding-from-uri/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM