环境结果配置: vs2019 +.netCore3.1
1.新建项目
打开vs --> 新建项目Asp.Net.Core Web应用程序 --> 选择API --> 创建
2.创建打开默认界面控制器(可跳过该步骤)
新建控制器HomeController
[Route("/")] [ApiController] public class HomeController : ControllerBase { public IActionResult Index() { return new JsonResult("besos Server api"); } }
打开Properties文件夹中的launchSettings.json文件
3.创建接口例子的控制器
新建控制器UserController
[Route("api/[controller]/[action]")] // 定义路由规则 [ApiController] public class UserController : ControllerBase { [HttpGet] public IActionResult GetUsers() { List<User> users = new List<User>(); User user = new User() { Name = "张三", Gender = "男", Age = 18 }; User user1 = new User() { Name = "李四", Gender = "女", Age = 8 }; User user2 = new User() { Name = "王五", Gender = "女", Age = 25 }; User user3 = new User() { Name = "赵六", Gender = "男", Age = 30 }; users.Add(user); users.Add(user1); users.Add(user2); users.Add(user3); return new JsonResult(users); } }
4.运行项目
5.浏览器打开 https://localhost:44364/api/User/GetUsers
6.网页运行结果