我正在使用带有 net-core API 控制器的 react-table,并且在抓取“排序”和“过滤”字段时遇到了一些麻烦。
我正在“onFetchData”方法中发送字段,如下所示:
Axios.get('/api/Dashboard/GetGiftCards', {
params: {
page: state.page,
pageSize: state.pageSize,
sorted: state.sorted,
filtered: state.filtered
}
})
例如,发送的包含 3 种排序类型和 3 个过滤器的查询字符串如下所示:
http://localhost:64963/api/Dashboard/GetGiftCards?page=0&pageSize=10&sorted[]=%7B%22id%22:%22giftCardType%22,%22desc%22:false%7D&sorted[]=%7B%22id%22:%22membershipId%22,%22desc%22:false%7D&sorted[]=%7B%22id%22:%22createdDate%22,%22desc%22:false%7D&filtered[]=%7B%22id%22:%22imisId%22,%22value%22:%223%22%7D&filtered[]=%7B%22id%22:%22giftCardType%22,%22value%22:%22E%22%7D
服务器端,我的控制器设置如下:
[HttpGet("GetGiftCards")]
public async Task<IActionResult> GetCardsAsync([FromQuery] GetGiftCardsRequest request)
我的任何请求对象如下
public class GetGiftCardsRequest
{
[JsonProperty(PropertyName = "page")]
public int Page { get; set; }
[JsonProperty(PropertyName = "pageSize")]
public int PageSize { get; set; }
[JsonProperty(PropertyName = "sorted")]
public IEnumerable<string> sorted { get; set; }
[JsonProperty(PropertyName = "filtered")]
public string[] Filters { get; set; }
public class Sorting
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "desc")]
public bool Descending { get; set; }
}
// Filtering object not created yet
}
我正在努力让控制器使用 URL,但到目前为止我还没有在网上找到任何东西。我在想我可能应该只使用 Regex 构建自己的自定义过滤器,但我想我会先在这里发布,看看是否有其他人提出了解决方案?