Json method not recognized in web api controller

I have a web api controller

using sport.BLL.Abstract; using sport.BLL.Concrete; using sport.DAL.Entities; using sport.webApi.Models; using AutoMapper; using Microsoft.AspNet.Identity.EntityFramework; using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using Microsoft.Owin.Security; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web; using System.Web.WebPages.Html; namespace sport.webApi.Controllers { public class AccountManageController : ApiController { [HttpPost] public System.Web.Mvc.ActionResult CreateAccount(CollaborateurModel item) { var user = new ApplicationUser { UserName = item.Username, Email = item.Email }; var result = UserManager.CreateAsync(user, item.Password); if (result.Result.Succeeded) { var currentUser = UserManager.FindByName(item.Username); var roleresult = UserManager.AddToRole(currentUser.Id, item.Role); ajt_collaborator entity = Mapper.Map<CollaborateurModel, ajt_collaborator>(item); entity.id_user_fk = currentUser.Id; entity.is_deleted = false; repo.CreateCollaborator(entity); var response = new { Success = true }; return Json(response); } else { var errorResponse = new { Success = false, ErrorMessage = "error" }; return Json(errorResponse); } } } } 

An error occurred on this line:

return Json (response);

Json method is not recognized !!! when I googled that I get a link that indicates that the Json method Json included in System.Web.Mvc . Even I'm trying to import this namespace, am I getting the same error?

  • What is the reason for this error?
  • How can i fix this?
+6
source share
2 answers

The problem is that you are inheriting from ApiController , but Json is a member of System.Web.Mvc.Controller .

Try using JsonResult :

 return new JsonResult { data = yourData; } 

You can set any object to data as it will be serialized in JSON.

For example, if you need to return only the result of the operation, you can use it as follows:

 return new JsonResult { data = true; } // or false 

However, it is good practice to describe the class of the result and the returned objects.

+6
source

How can i fix this?

The reason is that you do not inherit from Controller , but from ApiController , where the first one has Json(object o) as the method in it of the base class, but this does not really matter, since there is a fundamental problem with your approach.

ApiController , which is used with WebAPI, is not intended to return an ActionResult , which is a concept owned by MVC. Instead, you simply return your POCO and let the WebAPI framework handle the serialization for you:

 public object CreateAccount(CollaborateurModel item) { // Do stuff: if (result.Result.Succeeded) { return new { Success = true } } else { return new { Success = false, ErrorMessage = "error" }; } } 

You can set the configuration

+5
source

Source: https://habr.com/ru/post/988767/


All Articles