简体   繁体   English

如何在asp.net核心web api中阅读发布的FormUrlEncodedContent?

[英]how to read posted FormUrlEncodedContent in asp.net core web api?

in asp.net core web api i have a POST method that get username and password in FormUrlEncodedContent format from client. asp.net core web api我有一个POST方法,从客户端获取FormUrlEncodedContent格式的用户名和密码。 But the "entry" parameter is null . “entry”参数为null

how can i access the username and password in Login method in web api ? 如何在web api中的Login方法中访问用户名和密码?

here is my code: 这是我的代码:

client: 客户:

public async Task<string> login2(string command , string username , string password)
   {
        string exist = string.Empty;

        FormUrlEncodedContent dataForm = new FormUrlEncodedContent(new[] {
            new KeyValuePair<string,string>("username",username),
            new KeyValuePair<string, string>("password",password)
        });

        var resp = await http.PostAsync(command,dataForm);
        exist = await resp.Content.ReadAsStringAsync();
        return exist;
   }

server : 服务器:

public IActionResult Login([FromForm] string entry)
  {
       if (!ModelState.IsValid)
         {
             return BadRequest(ModelState);
         }

       Console.WriteLine(entry);

       return Ok(entry);
  }

Create a model to hold the posted information 创建模型以保存发布的信息

public class LoginModel {
    public string username { get; set; }
    public string password { get; set; }
}

and update the action to expect that from the from 并更新动作以期望来自

public IActionResult Login([FromForm] LoginModel entry) {
    if (!ModelState.IsValid) {
        return BadRequest(ModelState);
    }
    // access the username and password
    var username = entry.username;
    var password = entry.password;

    return Ok();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何接收发布到 ASP.net 核心 2.1 Web-API 端点的文件和数据 - How to receive both File and data POSTed to an ASP.net core 2.1 Web-API endpoint 具有集合属性的类在作为 XML 发布到 ASP.Net Core 3.1 Web API 时未正确绑定 - Class with Collection Properties are not binding properly when posted as XML to ASP.Net Core 3.1 Web API ASP.NET Core Web API无法接收发布的值:始终为null - ASP.NET Core Web API cannot receive posted values: always null 如何在ASP.NET Core 6.0 Web API中读取Azure App服务的应用设置 - How to read application setting of Azure App service in ASP.NET Core 6.0 Web API ASP.NET Web API没有获得新发布的项目 - ASP.NET Web API not getting newly posted item ASP.Net Web API - 同步获取POSTed文件? - ASP.Net Web API - Get POSTed file synchronously? 如何捕获和保存发布到Resting Asp.net Web API的JSON数据 - How to catch and save json data posted to restful asp.net web api 如何从 ASP.NET 核心 mvc 向 ASP.NET 核心中的 Web API 发出 PUT 请求? - How to make a PUT request from ASP.NET core mvc to Web API in asp.net core? 如何在asp.net core 2中验证facebook web api - How to authenticate facebook web api in asp.net core 2 如何在ASP.NET Core动态Web API中使用HttpGet? - How to use HttpGet in ASP.NET Core dynamic web api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM