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

S.L.javad

in asp.net core web api i have a POST method that get username and password in FormUrlEncodedContent format from client. But the "entry" parameter is null.

how can i access the username and password in Login method in web api ?

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);
  }
Nkosi

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();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to receive both File and data POSTed to an ASP.net core 2.1 Web-API endpoint

ASP.NET Core 7 Web API cannot receive posted values: always null

read JSON value in ASP.NET Core web api

How to read application setting of Azure App service in ASP.NET Core 6.0 Web API

How to catch and save json data posted to restful asp.net web api

how to read header data from web api response in .net core?

How to read json file in asp.net web api

How to read XML from ASP.NET Web API?

ASP.NET How read a multipart form data in Web API?

How to read ASP.NET_SessionId in Web API Controllers?

How to authenticate facebook web api in asp.net core 2

How to enable CORS globally in ASP.NET web API core

ASP.NET Core 8 Web API : how to add versioning?

How to Write connection string in asp.net core Web Api?

How does ASP.NET Core Web API build URLs?

How to call SQL Queries in ASP .NET Core Web API

How do you Authorize a Web API Controller in ASP Net Core

How to post list of object in ASP.NET Core Web API

How to properly convert a list to IQueryable on a asp net core web API

How to implement search filter in ASP.NET Core Web API

ASP.Net web api Vs .Net core web api

ASP.Net Core routes with Web API

ASP.NET Core Web API Authentication

debugging the asp.net core web API

Multiple MemoryCache in ASp .Net Core Web API

ASP.NET Core Identity in a Web API

How to check if user is logged in to ASP.NET Core web application when using ASP.NET Core Web API to house identity

How to return XML from ASP.NET 4 Web API to ASP.NET Core 2 application?

How to seperate ASP.NET MVC core view and ASP.NET WEB API controllers into separate projects?