Asp.net Dotnet core 3.1未绑定查询参数
调用HTTP get方法时,未正确填写查询参数 让我用代码解释一下Asp.net Dotnet core 3.1未绑定查询参数,asp.net,asp.net-mvc,asp.net-core,model-binding,query-parameters,Asp.net,Asp.net Mvc,Asp.net Core,Model Binding,Query Parameters,调用HTTP get方法时,未正确填写查询参数 让我用代码解释一下 [HttpGet] public async Task<IActionResult> GetTasks(Guid propertyId, [FromQuery] TaskFilterParams filterParams, [FromQuery] SortingParams sortingParams, [FromQuery] PagingParams pagingParams) { // Do some stuf
[HttpGet]
public async Task<IActionResult> GetTasks(Guid propertyId, [FromQuery] TaskFilterParams filterParams, [FromQuery] SortingParams sortingParams, [FromQuery] PagingParams pagingParams)
{
// Do some stuff, not important
}
更新:
我们添加了更多日志记录。数据似乎存在于查询字符串中,但模型没有正确绑定
Log.Debug("Query: {@query}", HttpContext.Request.Query);
Log.Debug(
"Get tasks parameters: PropertyId:{@propertyId}, TaskFilterParams:{@filterParams}, SortingParams:{@sortingParams}, PagingParams:{@pagingParams}",
propertyId, filterParams, sortingParams, pagingParams);
因为没有人对Stackoverflow有答案,所以我们在Github上提出了一个问题。。。
public class TaskFilterParams
{
public FilterParam<IssueCategory> Category { get; set; }
public FilterParam<RoomClass> RoomClass { get; set; }
public FilterParam<Guid?> Room { get; set; }
}
public abstract class FilterParam
{
public FilterOperator Operator { get; set; }
}
[TypeDescriptionProvider(typeof(FilterParamTypeDescriptionProvider))]
[JsonObject]
public class FilterParam<T> : FilterParam
{
public T Value { get; set; }
public static bool TryParse(string s, out FilterParam<T> result)
{
result = default(FilterParam<T>);
var filterOperators = Enum.GetValues(typeof(FilterOperator)).Cast<FilterOperator>();
foreach (var filterOperatorString in filterOperators.Select<FilterOperator, string>(x => x.ToFilterOperatorString()).OrderByDescending(x => x.Length))
{
if (s.StartsWith(filterOperatorString))
{
if (s.TryParse(filterOperatorString, out result))
{
return true;
}
return false;
}
}
return false;
}
}
public enum IssueCategory
{
Cleaning = 0,
Inspection = 1,
Maintenance = 2
}
public enum RoomClass
{
Single = 0,
Double = 1,
Family = 2
}
Log.Debug("Query: {@query}", HttpContext.Request.Query);
Log.Debug(
"Get tasks parameters: PropertyId:{@propertyId}, TaskFilterParams:{@filterParams}, SortingParams:{@sortingParams}, PagingParams:{@pagingParams}",
propertyId, filterParams, sortingParams, pagingParams);
[16:51:35 DBG] Query: [{"Key": "category", "Value": ["Cleaning"], "$type": "KeyValuePair`2"}, {"Key": "roomClass", "Value": ["Single"], "$type": "KeyValuePair`2"}]
[16:51:35 DBG] Get tasks parameters: PropertyId:[some-guid], TaskFilterParams:{"Category": null, "RoomClass": {"Value": "Single", "Operator": "Equal", "$type": "FilterParam`1"}, "$type": "TaskFilterParams"}