如何在同一个API方法中接收两个不同的对象?

问题描述 投票:0回答:1

我有一个实现 POST 方法的 API .NET 7。 此 POST 将作为 Webhook 工作,可以接收两个不同的请求正文。由于我无法控制请求,因此我无法指定具体的类作为方法的参数。

我的控制器:

//Receive a generic object 
[HttpPost]
public IActionResult Post (object request)
{
  try{
      //should identity the object type, convert to concrete class and call the service
      return Ok();
  }
  catch(Exception ex)
  {
      return BadRequest(ex);
  }
}

可能的请求主体已经定义为数据模型:

public partial class SignupMatch
{
    public string Id { get; set; }
    public string Status { get; set; }
    public string Name { get; set; }
    public DateTimeOffset DateCreated { get; set; }
    public DateTimeOffset LastUpdated { get; set; }
    public Match[] Matches { get; set; }
}

还有:

public class Proposal
{
    public string mother { get; set; }
    public string gender { get; set; }
    public string nationality { get; set; }
    public string hometownState { get; set; }
    public string hometown { get; set; }
    public string education { get; set; }
    public string relationshipStatus { get; set; }
    public string phoneLandline { get; set; }
}

如何根据请求识别我收到的对象?

c# .net generics webhooks
1个回答
0
投票

你尝试过这样的事情吗?

在您的program/startup.cs文件中尝试添加

app.MapControllerRoute(name: "SignupMatch",
                pattern: "SignupMatch",
                defaults: new { controller = "<whatever your controller name is>", action = "SignupMatch" });
app.MapControllerRoute(name: "Proposal",
                pattern: "Proposal",
                defaults: new { controller = "<whatever your controller name is>", action = "Proposal" });

想法取自(ASP.NET Core 中路由到控制器操作)[https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-7.0]

© www.soinside.com 2019 - 2024. All rights reserved.