dotnetcoder
dotnetcoder

Reputation: 31

error in webapi - cannot convert from 'System.Web.Mvc.JsonRequestBehavior' to 'Newtonsoft.Json.JsonSerializerSettings'

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

Answers (3)

Md Shahriar
Md Shahriar

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

Enrique
Enrique

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

Umair Akbar
Umair Akbar

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

Related Questions