Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
When I request From POSTMAN with row json its working fine
Ex -when i pass siple 1 its working but when i send this like {"id":"1"} m not getting the result

What I have tried:

C#
namespace DemoApi.Controllers
{
    public class EmployeeController : ApiController
    {       
        IList employees = new List() 
        {
           new Employee()  
                {  
                    EmployeeId = 1, EmployeeName = "Mukesh Kumar", Address = "New Delhi", Department = "IT"  
                },  
                new Employee()  
                {  
                    EmployeeId = 2, EmployeeName = "Banky Chamber", Address = "London", Department = "HR"  
                },  
                new Employee()  
                {  
                    EmployeeId = 3, EmployeeName = "Rahul Rathor", Address = "Laxmi Nagar", Department = "IT"  
                },  
                new Employee()  
                {  
                    EmployeeId = 4, EmployeeName = "YaduVeer Singh", Address = "Goa", Department = "Sales"  
                },  
                new Employee()  
                {  
                    EmployeeId = 5, EmployeeName = "Manish Sharma", Address = "New Delhi", Department = "HR"  
                },  
        };

        public IList GetAllEmployees()
        {
            //Return list of all employees  
            return employees;
        }       

        [HttpPost]       
        public Employee GetData([FromBody] int id)
        {
            var employee = employees.FirstOrDefault(e => e.EmployeeId == id);

            if (employee == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return employee;
        }

    }
}
Posted
Updated 17-Mar-20 11:13am
v2
Comments
Richard Deeming 20-Jan-17 9:27am    
Try removing the [FromBody] attribute on your id parameter.

1 solution

Using [FromBody]

To force Web API to read a simple type from the request body, add the [FromBody] attribute to the parameter:

public HttpResponseMessage Post([FromBody] string name) { ... }
In this example, Web API will use a media-type formatter to read the value of name from the request body. Here is an example client request.

POST http://localhost:5076/api/values HTTP/1.1
User-Agent: Fiddler
Host: localhost:5076
Content-Type: application/json
Content-Length: 7
"Alice"

When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).

At most one parameter is allowed to read from the message body. So this will not work:

// Caution: Will not work!
C#
public HttpResponseMessage Post([FromBody] int id, [FromBody] string name) { ... }


The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once.

Read more here: Parameter Binding in ASP.NET Web API | The ASP.NET Site[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900