gpt4 book ai didi

GET vs. POST Json response and DTO(获取与发布Json响应和DTO)

转载 作者:bug小助手 更新时间:2023-10-24 20:33:56 24 4
gpt4 key购买 nike



I am programming a C# Blazor Web application.

我正在编写一个C#Blazor Web应用程序。


GET--- I typically communicate with my server side controllers using a GET if I wish a response that then deserializes into a C# object I can use client side. I use the URL append to send parameters to my controller from which to create the response to the GET.

Get-我通常使用Get与服务器端控制器通信,如果我想要一个响应,然后将其反序列化为C#对象,我可以使用客户端。我使用URL APPEND将参数发送到我的控制器,从中创建对GET的响应。


POST--- I typically communicate with my controller using a POST if I wish to send back a Data Transfer Object for the server to do something with and all I wish is a response (i.e. success)

POST-我通常使用POST与我的控制器通信,如果我希望发回数据传输对象以供服务器执行某些操作,而我希望的只是一个响应(即成功)


Presently though, I wish to use a DTO to send some serialized information back to the controller and want to receive a response that then deserializes into a C# object.

不过,目前我希望使用DTO将一些序列化的信息发送回控制器,并希望接收一个响应,然后将该响应反序列化为C#对象。


The issue I have is that the code below shows lines from two different httpclients of mine showing how I can put my 'obj' variable which is my DTO into a PostAsJsonAsync, however, the GetFromJsonAsync doesn't allow me to send an object over (note these two lines are in separate httpclients... just put together to show them). How do others accomplish the task of sending a DTO and receiving more than a simple Response Message back?

我的问题是,下面的代码显示了来自我的两个不同Http客户端的代码行,显示了如何将我的‘obj’变量(即我的DTO)放入PostAsJsonAsync中,然而,GetFromJsonAsync不允许我发送对象(请注意,这两行位于不同的Http客户端中……只是放在一起向他们展示)。其他人如何完成发送DTO并接收不止一条简单的响应消息的任务?


    var rtnJson = await _http.GetFromJsonAsync<List<ChartPoint>?>(requestUri.ToString(),token);
var httpResponse= await _http.PostAsJsonAsync(requestUri, obj, token);

更多回答

A fellow coder suggested this as a solution... ``` var httpResponse= await _http.PostAsJsonAsync(requestUri, obj, token); return await httpResponse.Content.ReadFromJsonAsync<List<ChartPoint>>(cancellationToken:token); ```

另一位程序员建议这是一种解决方案。`var HttpResponse=AWAIT_Http.PostAsJsonAsync(questUri,obj,httpResponse.Content.ReadFromJsonAsync>(cancellationToken:token););返回等待Token``

优秀答案推荐

See comment above. The overload PostAsJsonAsync syntax allows return of a type specified by the controller.

请参阅上面的备注。重载的PostAsJsonAsync语法允许返回控制器指定的类型。


更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com