在Web API中调用PUT方法时出现以下错误? [英] Getting following error while calling the PUT method in web API?
本文介绍了在Web API中调用PUT方法时出现以下错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1]参数字典包含方法'System.Net.Http.HttpResponseMessage Put(Int32,ClassLibrary2.Employee)的非可空类型'System.Int32'的参数'id'的空条目''在'EmployeeService.Controllers.EmployeeController'中。可选参数必须是引用类型,可空类型,或者声明为可选参数。
2]第二次错误我的ID值为0
请参阅以下COde: -
我尝试过:
public HttpResponseMessage Put([FromBody] int id, [FromUri]员工员工)
{
try
{
using(EmployeeDBEntities entities = new EmployeeDBEntities())
{
var entity = entities.Employees .FirstOrDefault(e => e.ID == id);
if(entity == null)
{
返回Request.CreateErrorResponse(HttpStatusCode.NotFound,
带有Id的员工+ id.ToString()+未找到更新);
}
else
{
entity.FirstName = employee.FirstName;
entity.LastName = employee.LastName;
entity.Gender = employee.Gender;
entity.Salary = employee.Salary;
entities.SaveChanges();
返回Request.CreateResponse(HttpStatusCode.OK,entity);
}
}
}
catch(Exception ex)
{
返回Request.CreateErrorResponse(HttpStatusCode.BadRequest,ex);
}
}
请查找WebApiConfig.Cs文件: - < br $>
public static void Register(HttpConfiguration config)
{
// Web API配置和服务
// Web API路由
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();
}
解决方案
您的Put命令是什么样的?
错误正在尝试告诉你你没有在它认为匹配id参数的Put命令中发送的值。
[FromBody]属性意味着它正在寻找发布数据正文中的数据(不在参数字符串中?id = 77或其他)。
在WebAPI方法上,将属性从[FromBody]更改为[FromUri]。
< pre lang =c#> [FromUri()] int id
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(); }
解决方案
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).
On your WebAPI method change the attribute from [FromBody] to [FromUri].
[FromUri()] int id
这篇关于在Web API中调用PUT方法时出现以下错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文