首页 > 解决方案 > 如何使用 C# 检索传入的参数?

问题描述

我正在使用 ASP.NET Core MVC 2.0与 waboxapp API(链接)集成。

一些参数已经像这样发布了

contact[uid], contact[name], contact[type], message[uid], message[body] etc...

我尝试了以下代码:

 [HttpPost]
 public IActionResult Index(string uid, string token, List<string> contact)
 {
     foreach (string item in contact) {
         Common.TestEmail(uid, token);
     }

     return View();
 }

检索传入参数的正确方法是什么?

标签: c#jsonapiasp.net-core

解决方案


对于waboxapp,它的请求是标准 HTTP 格式 (application/x-www-form-urlencoded)。尝试按照以下步骤操作:

  1. 模型

    public class Waboxapp
    {
        public string Token { get; set; }
        public Contact Contact { get; set; }
    }
    public class Contact
    {
        public string Name { get; set; }
        public string Type { get; set; }
    }
    
  2. 行动

        [HttpPost]
    public IActionResult WaboxappFromForm([FromForm]Waboxapp waboxapp)
    {
        return View();
    }
    
  3. 要求
    在此处输入图像描述

  4. 结果 在此处输入图像描述


推荐阅读