铸造复杂的查询字符串到[FromQuery]模型

发布于 2025-02-05 09:57:34 字数 1079 浏览 2 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

养猫人 2025-02-12 09:57:34

好的,所以您的要求应该是您的输入请求具有参数,例如?xxx&amp; filt = {“ xx”:“ xxx”}&amp; ...,它是JSON格式的,因此应将其读为字符串,然后尝试将其转换为JSON对象。

==============================

i.sstatic.net/pmxjp.png“ rel =“ nofollow noreferrer”>

我认为它应该起作用。

    [ApiController]
    [Route("Hello")]
    public class HelloController : ControllerBase
    {
        [HttpGet]
        public string Get([FromQuery]Movie mov)
        {
            var a = mov.Title;
            return "success";
        }
    }

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.

enter image description here

=================================

enter image description here

======================================

I think it should work.

    [ApiController]
    [Route("Hello")]
    public class HelloController : ControllerBase
    {
        [HttpGet]
        public string Get([FromQuery]Movie mov)
        {
            var a = mov.Title;
            return "success";
        }
    }

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文