Получение следующей ошибки при вызове метода PUT в web API?
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:-
Что я уже пробовал:
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); } }
Пожалуйста, Найдите WebApiConfig.Cs файл:-
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(); }