【问题标题】:How to create a POST method that receives a list of strings in ASP.NET MVC?如何在 ASP.NET MVC 中创建一个接收字符串列表的 POST 方法?
【发布时间】:2021-01-13 12:46:32
【问题描述】:

在我的 android 应用程序中实现之前,我正在使用 Postman 进行一些测试。 我在 ASP.NET MVC 中创建了一个 WebAPI,并创建了一个将位置添加到数据库的方法。为此,我得到了一个字符串列表,但总是出现一个错误,指出该方法不存在。我做错了什么?

我的代码:

  public class LocalNotificacaoController : ApiController
    {
        private TransEntities db = new TransEntities();

    // POST: api/LocalNotificacao
        public bool PostNotificacao(List<string> locais, string emailUser, string tipo_login, string hash)
        {
            if (!ModelState.IsValid)
            {
                return false;
            }
    
            //will go through each location
    
            foreach (string local in locais)
            {
    
                //will check if the location already exists
    
                var PlaceExists = db.Find_Local_Notificacao(local).ToList();
    
    
                //if the location does not exist, it will add the location to the Notification table

                if (PlaceExists.Count == 0)
                {
                    //add place
                    db.Add_Local_Notificacao(local);
                }
    
                //get location id
                var res = db.Get_IDLocal_Notificacao(local);
                int id = 0;
                foreach (var item in res)
                {
                    id = item.Value;
                }
    
                //add location and user to the Notification-User table
                db.Add_Notificacao_Utilizador(id, emailUser, tipo_login);
                db.SaveChanges();
    
                return true;
            }
            return false;
        }
    }
}

邮递员

【问题讨论】:

  • 不确定查询字符串中的多个locais 能否正常工作。在将方法标记为HttpPost 之后(您也可以尝试将您的方法命名为Post),您可能需要将查询字符串更改为locais[0]=str&amp;locais[1]=str1.....

标签: c# asp.net-mvc postman


【解决方案1】:

您尝试在 Postman 中调用的内容存在一些问题,但您需要做的第一件事是将 HttpPost 属性添加到 PostNotificacao。如果没有这个,它将处理GET 请求,因此尝试POST 将无法正常工作:

// POST: api/LocalNotificacao
[HttpPost]
public bool PostNotificacao([FromUri] List<string> locais, string emailUser, string tipo_login, string hash)

默认路由配置可以在App_Start\WebApiConfig 类中找到。它应该是这样的:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

除非您已更改默认路由以在所有内容前加上 WebApi,否则在尝试调用控制器时无需在 Postman 中添加 WebApi。综上所述,您应该在 Postman 中使用的 URL 将类似于:

https://localhost:44351/api/LocalNotificacao?locais[0]=test1&locais[1]=test2&emailUser=hi@me.com&tipo_login=cool&hash=blah

地点:

  1. localhost:44351 是您的基址
  2. /api/LocalNotificacao 是因为你正在调用一个名为 LocalNotificacaoController 的 API 控制器,它默认配置为在 /api 下运行
  3. locais[0]=test1&amp;locais[1]=test2 是将多个值绑定到 List&lt;string&gt; locais 集合需要执行的操作

另外,请注意:

if (!ModelState.IsValid)
{
    return false;
}

不会在你的代码中做任何事情,因为你只是使用原始类型作为你的方法的参数(例如string)。阅读documentation 以了解如何使用它。

【讨论】:

  • 非常感谢它的工作。我仍然是初学者,所以我遇到了一些问题,但我确实理解了。我在 url pk 中使用 WebApi 我正在通过 IP 访问该方法。并且将 url locais=test&locais = test1 以相同的方式工作。该方法是以错误的方式实现的。并感谢 ModelState.IsValid 的解释。
猜你喜欢
  • 2017-09-08
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多