2

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);
        }
6
  • Please hover over 'Json' and tell us where it says it's coming from. I suspect that there is some funny-ness, like a duplication of Json when it should be looking to the controller for it's Json method Commented May 26, 2017 at 14:59
  • enum system.System.Web.Mvc.JsonRequestBehaviour.may be it should be something like this " return Json((object)new { success = false });" Commented May 26, 2017 at 15:03
  • Possible duplicate of Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible? Commented May 26, 2017 at 15:12
  • I just tried it and it doesn't cause a compile error to put a bool in there. As long a Json is the pure ASP.NET MVC version of Json. Have you found out where Json is defined?? There must be a reason why your Json method accepts JsonSerializerSettings and not the normal JsonRequestBehaviour Commented May 26, 2017 at 15:12
  • can you select Json and go to definition, is the definition protected internal JsonResult Json(object data, JsonRequestBehavior behavior); Commented May 26, 2017 at 18:40

3 Answers 3

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
            };
Sign up to request clarification or add additional context in comments.

Comments

1

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);
    }
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.