【问题标题】:Json type is not found within asp.net web api application在 asp.net web api 应用程序中找不到 Json 类型
【发布时间】:2016-02-29 20:24:10
【问题描述】:

我的 asp.net web api 应用程序中有这个控制器:

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;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Net.Mail;


namespace AjTransport.webApi.Controllers 
{
    public class AccountManageController : ApiController
    {

        [AllowAnonymous]
        public System.Web.Mvc.JsonResult FindUser(string str)
        {
            var result = UserManager.FindByName(str) ??   UserManager.FindByEmail(str);
             return Json(result == null, System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }
}
}

问题来了

return Json(result == null, System.Web.Mvc.JsonRequestBehavior.AllowGet);

找不到类型 Json。所以我需要知道如何解决这个问题?

【问题讨论】:

    标签: c# asp.net json asp.net-mvc asp.net-web-api


    【解决方案1】:

    改变这个

    return Json(result == null, System.Web.Mvc.JsonRequestBehavior.AllowGet);
    

    return Json(result == null);
    

    也不要返回System.Web.Mvc.JsonResult,将其更改为。

    public System.Web.Http.Results.JsonResult<bool> FindUser(string str)
    

    System.Web.Mvc.JsonRequestBehavior.AllowGet 仅对 MVC 有效,对 WebAPI 无效(提示,请参阅您正在使用的类型的名称空间)。

    有关ApiController.Json 方法的更多信息,请参阅链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-20
      • 2011-02-02
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多