Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1]The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Net.Http.HttpResponseMessage Put(Int32, ClassLibrary2.Employee)' in 'EmployeeService.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

2]Second Error I am getting ID values as 0

Please refer Following COde:-


What I have tried:

public HttpResponseMessage Put([FromBody]int id, [FromUri]Employee employee)
        {
            try
            {
                using (EmployeeDBEntities entities = new EmployeeDBEntities())
                {
                    var entity = entities.Employees.FirstOrDefault(e => e.ID == id);
                    if (entity == null)
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.NotFound,
                            "Employee with Id " + id.ToString() + " not found to update");
                    }
                    else
                    {
                        entity.FirstName = employee.FirstName;
                        entity.LastName = employee.LastName;
                        entity.Gender = employee.Gender;
                        entity.Salary = employee.Salary;

                        entities.SaveChanges();

                        return Request.CreateResponse(HttpStatusCode.OK, entity);
                    }
                }
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }



Please Find WebApiConfig.Cs file:-

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

            config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver= new CamelCasePropertyNamesContractResolver();
        }
Posted
Updated 21-Oct-18 5:00am

What does your Put command look like?
The error is trying to tell you that you don't have a value sent in on the Put command that it thinks matches the id parameter.
The [FromBody] attribute means it's looking for the data in the body of the posted data (not in the parameter string ?id=77 or whatever).
 
Share this answer
 
Comments
dattaprasaddhuri 21-Oct-18 5:45am    
want to pass the parameter in the query string to web api put method.

Please Refer url Link:-

http://localhost:60206/api/employee?id=1&FirstName=abc&LastName=def&Gender=female&Salary=15000
On your WebAPI method change the attribute from [FromBody] to [FromUri].
C#
[FromUri()] int id
 
Share this answer
 
Comments
dattaprasaddhuri 25-Oct-18 1:12am    
@raddevus Thanks

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