Reputation: 31
in webapi jsonrequestbehaviour is not working , showing cannot convert 'System.Web.Mvc.JsonRequestBehavior' to 'Newtonsoft.Json.JsonSerializerSettings'
code
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);
}
Upvotes: 2
Views: 7135
Reputation: 2736
Change public class shahriarController : ApiController to public class shahriarController : Controller. You have to inherit from Controller, not from ApiController. It will solve the 'JsonRequestBehavior.AllowGet'.
public class shahriarController : Controller
{
public ActionResult Get()
{
List<Student> list = new List<Student>();
list.Add(new Student() { Name = "sjass", Roll = "5544" });
list.Add(new Student() { Name = "wwww", Roll = "0777" });
list.Add(new Student() { Name = "rrttt", Roll = "4355" });
return Json(list, JsonRequestBehavior.AllowGet);
}
}
Upvotes: 1
Reputation: 1
I had the same problem. Its turned out I had the class defined like " public class PersonalApiController : ApiController" instead of " public class PersonalApiController : Controller". Changing that resolved the problem.
Upvotes: 0
Reputation: 588
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
};
Upvotes: 5