I was trying to read the form url encoded body in ASP.net WebAPI (.Net 6). But I always gets null value from it, Am I doing it wrong ?
Here my endpoint code
public class TestObj
{
[FromForm(Name = "empId")]
public string? EmpId { get; set; }
[FromForm(Name = "firstName")]
public string? FirstName { get; set; }
[FromForm(Name = "lastName")]
public string? LastName { get; set; }
}
[HttpPost("Test")]
public string Test([FromForm] TestObj formData)
{
string? emp = formData.EmpId ?? "EmpId NULL";
string? fName = formData.FirstName ?? "FirstName NULL";
string? lName = formData.LastName ?? "LastName NULL";
return $"Hello World {emp} - {fName} - {lName}";
}
and here is my request
POST /MyController/v1/Test HTTP/1.1
User-Agent: PostmanRuntime/7.29.2
Accept: */*
Postman-Token: 89601ac3-bd53-4ec0-84db-5aebe7b5f007
Host: localhost:7177
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 40
empId=xxx&firstName=fName&lastName=lName
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Tue, 19 Jul 2022 03:41:09 GMT
Server: Kestrel
Transfer-Encoding: chunked
Hello World EmpId NULL - FirstName NULL - LastName NULL
Sorry for this the problem occurred because of my middleware.