JSON方法无法在网络API控制器认可 [英] Json method is not recognized in Web Api controller

查看:178
本文介绍了JSON方法无法在网络API控制器认可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页API控制器

 使用sport.BLL.Abstract;
使用sport.BLL.Concrete;
使用sport.DAL.Entities;
使用sport.webApi.Models;
使用AutoMapper;
使用Microsoft.AspNet.Identity.EntityFramework;
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.Owin;
使用Microsoft.Owin.Security;
使用System.Net;
使用System.Net.Http;
使用System.Web.Http;
使用的System.Web;
使用System.Web.WebPages.Html;

命名空间sport.webApi.Controllers
{
    公共类AccountManageController:ApiController
    {
        [HttpPost]
        公共System.Web.Mvc.ActionResult CreateAccount(CollaborateurModel项)
        {
            VAR用户=新ApplicationUser {用户名= item.Username,电子邮件= item.Email};
            VAR的结果= UserManager.CreateAsync(用户,item.Password);
            如果(result.Result.Succeeded)
            {

                VAR的currentUser = UserManager.FindByName(item.Username);
                VAR roleresult = UserManager.AddToRole(currentUser.Id,item.Role);
                ajt_collaborator实体= Mapper.Map< CollaborateurModel,ajt_collaborator>(项目);
                entity.id_user_fk = currentUser.Id;
                entity.is_deleted = FALSE;
                repo.CreateCollaborator(实体);
                VAR响应=新{成功= TRUE};
                返回JSON(响应);


            }
            其他
            {
                VAR errorResponse =新{成功=假,的ErrorMessage =错误};
                返回JSON(errorResponse);
            }

        }
    }
}
 

我在这一行错误:

返回JSON(响应);

JSON的方法是不承认!当我用Google搜索有关,我得到这个的链接这表明的Json 方法包括在 System.Web.Mvc 。即使我尝试导入该命名空间,我得到了同样的错误?

  1. 那么,这个错误的原因是什么?
  2. 我怎样才能解决这个问题?
解决方案

现在的问题是,你从 ApiController ,但的Json继承 System.Web.Mvc.Controller 中的一员。

尝试使用 JsonResult

 返回新JsonResult {数据= yourData; }
 

您可以设置任何对象数据,因为它会被序列化到JSON。

例如,如果你需要返回操作的只是一个结果,你可以用这种方式:

 返回新JsonResult {数据= TRUE; } //还是假的
 

然而,这是一个很好的做法,描述一个结果类,并返回对象。

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?

解决方案

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.

这篇关于JSON方法无法在网络API控制器认可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆