1

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);
        }
dotnetcoder
  • 21
  • 2
  • 10
  • 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 – Luke May 26 '17 at 14:59
  • enum system.System.Web.Mvc.JsonRequestBehaviour.may be it should be something like this " return Json((object)new { success = false });" – dotnetcoder May 26 '17 at 15:03
  • Possible duplicate of [Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?](https://stackoverflow.com/questions/7109967/using-json-net-as-the-default-json-serializer-in-asp-net-mvc-3-is-it-possible) – Majid Parvin May 26 '17 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` – Luke May 26 '17 at 15:12
  • can you select Json and go to definition, is the definition `protected internal JsonResult Json(object data, JsonRequestBehavior behavior);` – Munzer May 26 '17 at 18:40
  • protected internal JsonResult Json(T content); – dotnetcoder May 27 '17 at 06:40

3 Answers3

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
            };
Umair Akbar
  • 578
  • 5
  • 11
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.

0

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);
    }
}
Md Shahriar
  • 2,072
  • 22
  • 11