ASP.NET Core Web项目编写API并返回结构化Json

从ASP.NET MVC(.NET Framework)开发转为ASP.NET Core MVC(.NET Core)开发需要一个逐步摸索的过程,因为微软为了实现跨平台而重写了所有的dll,而且许多接口方法也都重写了。

我用的是Visual Studio 2019 Enterprise

【1】首先,选择项目类型并创建

【2】选择项目模版(虽然我们开发的是API项目,但是因为模式选择的还是MVC,所以模板还是选择MVC项目)

【3】创建实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace core1.Models
{
    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Phonenumber { get; set; }
        public string Password { get; set; }
        public DateTime CreateTime { get; set; }
        public string Remark { get; set; }
        public string ImageUrl { get; set; }
        public string Feedback { get; set; }
    }
}

【4】创建数据库上下文(注:我的数据库和表是之前已经创建好的)

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace core1.Models
{
    public class DbEntity : DbContext
    {
        public DbSet<User> Users { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"data source=/*数据库地址*/;initial catalog=/*数据库名称*/;User Id=/*数据库账户名*/;Password=/*数据库密码*/ ");

        }

    }
}

【5】创建Controller

     所有创建的Controller都是默认继承ControllerBase,这是所有MVC控制器的基类,经过测试发现不支持重写Dispose方法和Newtonsoft.Json,之后我选择将ControllerBase改为Controller(其实也就是选择了MVCController),完美兼容,在ASP.NET MVC中,我通常使用IHttpActionResult这一数据返回类型返回Json格式数据,但在Core中显然不支持,所以我更换为了IActionResult。  

     此外,我还将类上方的路径格式从

[Route("api/[controller]")]

改为

[Route("api/[controller]/[action]")]

否则无法访问特定的方法。

完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using core1.Models;
using System.Net.Http;
using Parttime.Helper;

namespace core1.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UsersController : Controller
    {
        private DbEntity db = new DbEntity();
        protected override void Dispose(bool disposing)
        {
            if (disposing) db.Dispose();
            base.Dispose(disposing);
        }

        //[Route("Users")]
        public IActionResult GetUsers()
        {
            List<User> users = db.Users.ToList();
            return Json(new
            {
                datas =users.Select(s=>new
                {
                    s.Id,
                    s.Username,
                    s.Phonenumber,
                    s.Password,
                    s.CreateTime,
                    s.ImageUrl
                })
            });
        }
    }
}

返回类型也可以使用其他兼容Json的类型。

【6】接口测试

利用Postman对编写好的接口进行测试:

可以发现,数据成功获取到了。

最近在学习Core和Docker,后面还会继续与大家分享。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

越来越胖的GuanRunwei

祝老板们身体健康,财源广进!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值