铸造复杂的查询字符串到[FromQuery]模型
我在Donetcore项目中的WebAPI中有[HTTPGET]方法。 在客户端,我将这种方法称为复杂的查询字符串,例如Bellow
descending=false&page=1&rowsPerPage=20&Filters=%7B%22ProductId%22:14125F3D-3662-45DC-895C-691911A4767B%22%22%7D&Search=
和Controller的操作,我具有此端点:
[HttpGet("GetPaginated")]
public async Task<IActionResult> GetPaginated([FromQuery] BundleProductPaginatedRequest
model,CancellationToken cancellationToken)
{
}
并且BundleProductauctPaginedRequestRequest继承了该类,其中包含ProductID,
public class PaginationRequest<T> where T : IAdvancedFilter?, new()
{
public int? Page { get; set; }
public int? RowsPerPage { get; set; }
public string? SortBy { get; set; }
public bool Descending { set; get; }
public string? Search { set; get; }
public T? Filters { get; set; } = new ();
}
我知道我可以从复杂方法中使用[HTTPPPOST]。但是此操作是[httpget]方法。 我还创建了自定义模型活页夹以将Querystring施加到字典中,然后转到JSON,然后将其用于我的模型: querystring-&gt;字典 - &gt; json-&gt; bundleproductauctauctAcationRequest。 我知道这是错误的方式。 所以请给我更好的解决方案
I Have [HttpGet] Method inside my WebApi in DoNetCore project.
at the client Side i call this method with complex query string such as bellow
descending=false&page=1&rowsPerPage=20&Filters=%7B%22ProductId%22:14125F3D-3662-45DC-895C-691911A4767B%22%22%7D&Search=
and in contrller's action i have this endpoint:
[HttpGet("GetPaginated")]
public async Task<IActionResult> GetPaginated([FromQuery] BundleProductPaginatedRequest
model,CancellationToken cancellationToken)
{
}
and BundleProductPaginatedRequest inherit this class that have ProductId inside that:
public class PaginationRequest<T> where T : IAdvancedFilter?, new()
{
public int? Page { get; set; }
public int? RowsPerPage { get; set; }
public string? SortBy { get; set; }
public bool Descending { set; get; }
public string? Search { set; get; }
public T? Filters { get; set; } = new ();
}
i Know i can use [HttpPost] from complex method. but this action is [HttpGet] Method.
i also create custom model binder to cast QueryString to dictionary and then to json and finaly to my model:
QueryString->Dictionary->Json->BundleProductPaginationrequest.
i know this is wrong way.
so please give me better solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以您的要求应该是您的输入请求具有参数,例如
?xxx&amp; filt = {“ xx”:“ xxx”}&amp; ...
,它是JSON格式的,因此应将其读为字符串,然后尝试将其转换为JSON对象。==============================
i.sstatic.net/pmxjp.png“ rel =“ nofollow noreferrer”>
我认为它应该起作用。
Ok, so your requirement should be that your input request has parameter like
?xxx&Filter={"xx":"xxx"}&...
, it's in Json format but it's recognized as string, so it should be read as string, then try to convert to Json object.=================================
======================================
I think it should work.