webapi 中的错误 - 无法从“System.Web.Mvc.JsonRequestBehavior”转换为“Newtonsoft.Json.JsonSerializerSettings”

dot*_*der 1 c# asp.net-mvc json asp.net-mvc-4 asp.net-web-api

在 webapi jsonrequestbehaviour 中不起作用,显示无法将“System.Web.Mvc.JsonRequestBehavior”转换为“Newtonsoft.Json.JsonSerializerSettings”

代码

 public ActionResult AddTemprature(string EmployeeName, int EmployeeId, string Location)
        {
            try
            {

                using (EmployeeDBEntities DB = new EmployeeDBEntities())
                {

                    WebApi.Employee emp = new WebApi.Employee();
                    // EmployeeModel Emp = new EmployeeModel();
                    emp.EmpName = EmployeeName;
                    emp.EmpId = EmployeeId;
                    emp.EmpLocation = Location;
                    DB.Employees.Add(emp);
                    DB.SaveChanges();
                    return Json(true, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception Ex)
            {

            }
            return Json(false, JsonRequestBehavior.AllowGet);
        }
Run Code Online (Sandbox Code Playgroud)

Uma*_*bar 5

You are using controller json respose, instead of ApiController response type. Use below code and you should be all set.

return new JsonResult()
            {
                Data = false,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
Run Code Online (Sandbox Code Playgroud)