发布方法未在数据库表Asp.net MVC Web API中添加值

我在Visual Studio MVC Web API中创建了一个Post action,并在Parameter中传递了tbl_employee,当我通过Postman检查action时,它会将空值插入数据库中的表中。

在Visual Studio 2019上工作,框架版本为4.5。

 public HttpResponseMessage Post([FromBody]tbl_employee stbl_Employee)
        {              
            try
            {  
                se.tbl_employee.Add(stbl_Employee);
                se.SaveChanges();
                var message = Request.CreateResponse(HttpStatusCode.Created,stbl_Employee);
                message.Headers.Location = new Uri(Request.Requesturi + 
                stbl_Employee.emp_id.ToString());
                return message;
            }
            catch (Exception ex)
            {
           return Request.CreateErrorResponse(HttpStatusCode.BadRequest,ex);

            }
        }  


 public partial class tbl_employee        
    {
        public int emp_id { get; set; }
        public string emp_name { get; set; }
        public Nullable<int> emp_salary { get; set; }
        public Nullable<int> emp_department { get; set; }

        public virtual tbl_department tbl_department { get; set; }
    }

输出为数据库中所有值均为空,而邮递员给出错误。

aukle 回答:发布方法未在数据库表Asp.net MVC Web API中添加值

我会检查您通过Postman发送到端点的JSON或XML格式。听起来您在反序列化HTTP请求的正文时可能会遇到问题。

可能值得考虑在项目中添加Swagger,以帮助您检查格式并测试api

本文链接:https://www.f2er.com/3160467.html

大家都在问