Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json method is not recognized in Web Api controller

I have an 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);
            }

        }
    }
}

I got an error in this line :

return Json(response);

the Json Method is not recognized!!! when I googled about that I get this link which indicates that Json method is included in System.Web.Mvc. Even I try to import this namespace I get the same error?

  1. So is the reason of this error?
  2. How can I fix it?
like image 243
Lamloumi Afif Avatar asked Jun 09 '15 08:06

Lamloumi Afif


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 a data as it will be serialized to a JSON.

For instance, if you need to return only a result of operation, you can use it this way:

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

However, it is a good practice to describe a result class and return objects.

like image 62
Yeldar Kurmangaliyev Avatar answered Sep 19 '22 19:09

Yeldar Kurmangaliyev


How can I fix it?

The reason is in the fact that you aren't inheriting from Controller but from ApiController, where the former has Json(object o) as a method in it's base class, but that doesn't actually matter, as there is a fundamental problem with your approach.

ApiController which is used with WebAPI isn't meant to return an ActionResult, which is a concept that belongs to MVC. Instead, you simply return your POCO and let the WebAPI framework handle 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 your formatter configuration in your Global.asax.cs file, and tell it exactly which ones to use (in your case, you'll want the JsonMediaTypeFormatter).

like image 33
Yuval Itzchakov Avatar answered Sep 21 '22 19:09

Yuval Itzchakov