Skip to main content
 首页 » 编程设计

c#之在 asp.net web api 应用程序中找不到 Json 类型

2024年01月26日28jillzhang

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

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 类型。所以我需要知道如何解决这个问题?

请您参考如下方法:

改变这个

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 无效(提示,查看您正在使用的类型的 namespace )。

有关 ApiController.Json 的更多信息方法见链接。