1、在WebApiConfig.cs中的代码注释掉。
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.Remove(config.Formatters.XmlFormatter);
//config.Formatters.Remove(config.Formatters.JsonFormatter);
//config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
//config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
}
2、在EmployeesController.cs中加入post代码。
public void Post([FromBody] Employee employee)
{
using (EmployeeDBEntities entities = new EmployeeDBEntities())
{
entities.Employees.Add(employee);
entities.SaveChanges();
//var message = Request.CreateResponse(HttpStatusCode.Created, employee);
//message.Headers.Location = new Uri(Request.RequestUri);
}
}
3、在postman中设置。
4、结果。