c# - 如何使用参数执行 HttpPost-action?

标签 c# api asp.net-core .net-core frombodyattribute

我有一个 API,其中包含

[HttpPost("InsertSomething")]
public async Task<IActionResult> InsertSomething(Guid id, [FromBody] MyModel model)
{
   try
   {
      ...
      Guid somethingId = await _myService.Insert(id, enz..)

当我想调用这个 Action 时

HttpResponseMessage result = await client.PostAsync("/MyContr/InsertSomething", content);

然后我会进入API的Action。

...当你添加这样的东西

HttpResponseMessage result = await client.PostAsync($"/MyContr/InsertSomething?id=BlaBlaBla", content);

然后我们就没有进入API的Action了。

如何解决这个问题?

最佳答案

您可以像这样更改端点:

 [HttpPost("InsertSomething")]
 public async Task<IActionResult> InsertSomething([FromQuery]Guid id, [FromBody] MyModel model)

另外,请确保您传递了正确的 GUID。

关于c# - 如何使用参数执行 HttpPost-action?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68106081/

相关文章:

c# - 具有 TimeSpan 数据类型的 DataView RowFilter

c# - 使用 Isolated COM 时应用程序在 CoCreateInstance 期间挂起

excel - 在其他功能之后运行UDF?

dependency-injection - 如何在多项目解决方案中使用 .net 核心依赖注入(inject)?

c# - 如何将 .NET 类库的引用添加到 vNext 项目

c# - 使用 C# 的 XML 注释 cref 属性和参数语法

c# - 将 WPF 应用程序作为 Windows 服务运行

api - Azure/Bing 图像搜索 API : How do I add multiple image filters?

java - 使用 Jax-Rs 在 RestApi 调用上返回用户定义的类对象时出错

c# - 使用 IOption 的类的实例化