Asp.net web api 在.netcore web api中将多个查询参数传递给GET端点
我有一个应用程序,其中将有几个参数传递到我的端点进行搜索,这些参数没有定义,因为它们是动态生成的,所以我无法将其映射到特定的模型。将任何查询参数映射到我的GET端点的最佳方法是什么Asp.net web api 在.netcore web api中将多个查询参数传递给GET端点,asp.net-web-api,asp.net-core,controller,Asp.net Web Api,Asp.net Core,Controller,我有一个应用程序,其中将有几个参数传递到我的端点进行搜索,这些参数没有定义,因为它们是动态生成的,所以我无法将其映射到特定的模型。将任何查询参数映射到我的GET端点的最佳方法是什么 [HttpGet] public CustomResponse GetResults({something here that will map the parameters?}) { //perhaps a dictionary? a collection of some sort? } 然后我需要获取
[HttpGet]
public CustomResponse GetResults({something here that will map the parameters?})
{
//perhaps a dictionary? a collection of some sort?
}
然后我需要获取所有这些键和值,并在数据库中搜索任何包含它们的内容,正如我所说的,它们可能是任何内容
这样我就可以通过这样的考试了
/api/Merchandise/GetResults?sku=30021&cupsize=medium&color=red&location=south& {and all the dynamic fields which could be anything}
你可以把它映射到JObject,它就像一本字典 别忘了:
using Newtonsoft.Json;
你可以把它映射到JObject,它就像一本字典 别忘了:
using Newtonsoft.Json;
HttpRequest
对象具有Query
属性,该属性是IQueryCollection
并保存所有传递的查询参数
换句话说,在您的行动方法中,您可以:
[HttpGet]
public CustomResponse GetResults()
{
var queryParams = HttpContext.Request.Query;
// directly get by name
var value1 = queryParams["parameter_name"];
// or queryParams.TryGetValue()
foreach (var parameter in queryParams)
{
string name = parameter.Key;
object value = parameter.Value;
}
}
HttpRequest
对象具有Query
属性,该属性是IQueryCollection
并保存所有传递的查询参数
换句话说,在您的行动方法中,您可以:
[HttpGet]
public CustomResponse GetResults()
{
var queryParams = HttpContext.Request.Query;
// directly get by name
var value1 = queryParams["parameter_name"];
// or queryParams.TryGetValue()
foreach (var parameter in queryParams)
{
string name = parameter.Key;
object value = parameter.Value;
}
}