.NET CORE MVC  返回 JSON 数据

1、在Startup.cs ConfigureService 中加入(需要通过Nuget 增加Microsoft.AspNetCore.Mvc.NewtonsoftJson)

  

services.AddControllersWithViews().AddNewtonsoftJson();

2、 控制器

 public IActionResult getRelevantSearch() 
        {
            string keyWord = Request.Query["keyword"];
            if (reg.IsMatch(keyWord))
                {

                var getData = bll.getRelevancySearch(keyWord);
                if (getData != null) 
                {
                 
                    return Json(new { code = 200, msg = "获取成功", data = getData });
                }  
                }
            return Json(new { code =400, msg = "获取失败", data="" });

        }

3、 效果:

原文地址:https://www.cnblogs.com/fogwang/p/12836281.html