1,创建ASP.NET Web API项目
生成完毕,项目结构如下:
运行看一下:
2,后台接口编写
(1)在Models文件夹中新建一个sandata.cs文件(就是上篇中武将信息表的model文件)
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace API.Models
{
public class Sandata
{
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[SugarColumn(IsPrimaryKey=true)]
public int Id {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string sex {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public int tongshuai {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public int wuli {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public int zhili {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public int zhengzhi {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public int meili {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string zhuyi {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string zhengce {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string zhengcedengji {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string peiou {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string name {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string gexing1 {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string gexing2 {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string gexing3 {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string gexing4 {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string gexing5 {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string shengnian {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string dengchang {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string zunian {get;set;}
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string hjqy { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string fdlm { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string eyyx { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string qxgj { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string gdzz { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string sgml { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string cbzz { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string hzzdz { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string gybww { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string csb { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string ztld { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string hdlb { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string hszc { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string hbys { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string cjfl { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string zzql { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string eftx { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string jmc { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string qfzh { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string yxjj { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string yxlw { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string yyyz { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string ccds { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string lbtfz { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string hbzz { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string tgzz { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string hfzz { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string ylzz { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string zfnm { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string qfwzy { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string zszb { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string shmw { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string htdl { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string hbgs { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
public string xsbl { get; set; }
/// <summary>
/// alias:
/// Default:
/// Nullable:True
/// </summary>
public string alias { get; set; }
/// <summary>
/// biography1:
/// Default:
/// Nullable:True
/// </summary>
public string biography1 { get; set; }
/// <summary>
/// biography2:
/// Default:
/// Nullable:True
/// </summary>
public string biography2 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string biography3 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string xiangxing { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string xueyuan { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string fuqin { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string muqin { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string shidai { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string yxd { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string zx { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string zf { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string like1 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string like2 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string like3 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string like4 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string like5 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string like6 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string like7 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string like8 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string dislike1 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string dislike2 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string dislike3 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string dislike4 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string dislike5 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string dislike6 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string dislike7 { get; set; }
/// <summary>
/// biography3:
/// Default:
/// Nullable:True
/// </summary>
public string dislike8 { get; set; }
public string qingyi { get; set; }
public string yexin { get; set; }
public string hanchao { get; set; }
public string haozhan { get; set; }
public string waijiao { get; set; }
public string wuzi { get; set; }
public string jianshe { get; set; }
}
}
(2)在Models文件夹中新建一个MessageModel.cs文件作为通用返回格式:
namespace API.Models
{
/// <summary>
/// 通用返回信息类
/// </summary>
public class MessageModel<T>
{
/// <summary>
/// 状态码
/// </summary>
public int status { get; set; } = 200;
/// <summary>
/// 页码
/// </summary>
public int page { get; set; }
/// <summary>
/// 数据条数
/// </summary>
public int pageCount { get; set; }
/// <summary>
/// 操作是否成功
/// </summary>
public bool success { get; set; } = false;
/// <summary>
/// 返回信息
/// </summary>
public string msg { get; set; } = "服务器异常";
/// <summary>
/// 返回数据集合
/// </summary>
public T response { get; set; }
}
}
(3)在Controllers文件夹里新建DataController.cs文件(新建控制器)
(4)使用sqlsugar这一款ORM框架来进行数据库操作
程序NuGet引入两个类库sqlsugar和Newtonsoft.Json
(5)Controllers文件夹下新建Helper.cs类
Helper.cs全部内容如下:
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace API.Helper
{
public class dbContext
{
private static SqlSugarClient _db = null;
/// <summary>
/// test是数据库名
/// </summary>
public static string ConnectionString = "server=localhost;uid=xxx;pwd=xxx;database=vue_test";
public static SqlSugarClient CretClient()
{
_db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = ConnectionString, //数据库连接字符串
DbType = DbType.MySql, //必填
IsAutoCloseConnection = false, //默认false
InitKeyType = InitKeyType.Attribute
});
return _db;
}
}
}
其中,DbType = DbType.MySql表示我们要连接的数据库为mysql,当然你也可以选择其他的数据库。或者切换多个不同的数据库(具体查看官方文档,既上面的sqlsugar),本篇中只使用一个数据库。server=localhost;uid=xxx;pwd=xxx;database=xxx为连接字符串,这是Mysql的连接字符串样式,不同的数据库是不同的,把里面的xxx换为你自己的数据库用户名,密码,使用的数据库名。
(6)打开我们之前写的DataController.cs文件添加接口方法
首先使用Helper.cs里新建的DbContext类来连接数据库
public static SqlSugarClient DbContext = dbContext.CretClient();
添加引用。此时,当前文件完整引用为:
using Newtonsoft.Json;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MySql.Data.MySqlClient;
using System.Collections;
using System.Text;
using System.Threading.Tasks;
using API.Models;
using System.Collections.Generic;
using System.Web;
using SqlSugar;
using System.Diagnostics;
using System.Reflection;
using API.Helper;
然后添加接口方法,例如添加一个分页查询的方法
/// <summary>
/// 获取数据(分页)
/// </summary>
///<param name="page">当前页数</param>
///<param name="intPageSize">每页显示数据条数</param>
///<param name="key">查询条件</param>
public async Task<MessageModel<object>> GetAsync(int page = 1,int intPageSize=10,string key="")
{
Expressionable<Data> exp = Expressionable.Create<SanData>();
if (!string.IsNullOrEmpty(key))
{
exp.And(it => it.Name.Contains(key));
}
var result = DbContext.Queryable<SanData>()
.Where(exp.ToExpression())
.ToPageList(page, intPageSize);
return await Task.FromResult(new MessageModel<object>()
{
status = 200,
success = true,
msg = "获取成功",
response = result
});
}
3,接口发布
(1)首先打包项目
项目上右键,选择“发布”
点击发布,目标位置里的app.publish文件夹就是我们后台打的包
(2)然后通过IIS发布
如果是新的服务器可能需要启用IIS服务
4,接口调用
注意1:接口在小程序使用之前,应保证可在外网访问。(这可在服务器平台的安全组设置以及服务器的出入口规则设置端口号来实现)
注意2:正式上线的小程序如要访问外网接口,接口必须是绑定在已经备案的域名上;并且,在小程序平台上的开发管理中,将开发设置下的服务器域名配置成你备案的域名。若是在本地微信开发者工具上访问,则先直接访问IP地址加端口号的形式也可以。
这里以获取亲爱/厌恶武将A的数据接口GetAllBeLove
为例:
请求url为:BASE_URL /GetAllBeLove
参数有两个,name
即武将名称,type
即亲爱/厌恶
在小程序中获取数据:
//获取亲爱武将A的数据
let name="刘备";
let type="like";
let result_like = await fetchData(BASE_URL + 'GetAllBeLove', { name, type });
//请求数据
async function fetchData(url, data = {}) {
return new Promise((resolve, reject) => {
wx.request({
url,
data,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: "GET",
success: resolve,
fail: reject
});
});
}
获取结果结构如下: