netCore3.0+webapi到前端vue(后端)
<p> </p><p>第一步创建api项目</p>
<p><div align="center"></div></p>
<p> </p>
<p> </p>
<p>创建完成启动F5!!</p>
<p>如图<div align="center"></div></p>
<p> </p>
<p>数据库我用的是mysql 用ef操纵数据 </p>
<p>开发环境:Win10 + VS2019<br />Mysql服务器版本:8.0.16</p>
<p> </p>
<p>1.下载并安装插件(必备)</p>
<p>MySQL-Connector-net-6.9.12 <br />MySQL for Visual Studio 2.0.5</p>
<p>用Nuget方式安装MySql.Data.Entity-6.9.12(MySql.Data.EntityFrameworkCore.Design!!两种都试过没问题,前面一种会报提示不兼容),MySql.Data-6.9.12 MySql.Data.EntityFrameworkCore<br />注意!!! 安装的2个dll版本号必须同等以及对应MySQL-Connector-net版本雷同</p>
<p><div align="center"></div></p>
<p> </p>
<p>2.在项目根目次新建Models文件</p>
<p>创建实体类 gj</p>
public class gj
{
// <summary>
/// 主键
/// </summary>
public int id { get; set; }
/// <summary>
/// 标题
/// </summary>
public string method { get; set; }
/// <summary>
/// 内容
/// </summary>
public string text { get; set; }
/// <summary>
/// 状态 1正常 0删除
/// </summary>
public string type { get; set; }
}
public class DbModel:DbContext
{
public DbSet<gj> gj { set; get; }
//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
// => optionsBuilder.UseMySQL(@"Server=localhost;database=testapp;uid=root;pwd=woshishui");
public DbModel(DbContextOptions<DbModel> options) : base(options)
{
}
}
<p> </p>
<p>appsettings.json 配置数据连接</p>
{
"ConnectionStrings": {
"DefaultConnection": "Server=xxxx;database=testapp;uid=root;pwd=xxxx;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
<p> </p>
<p>Startup.cs</p>
public void ConfigureServices(IServiceCollection services)
{
//连接 mysql 数据库,添加数据库上下文
services.AddDbContext<DbModel>(options =>
options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllers();
}
<p> </p>
<p>Controllers 下的ValuesController.cs</p>
<p>修改代码如下</p>
private readonly DbModel _coreDbContext;
public ValuesController(DbModel coreDbContext)
{
_coreDbContext = coreDbContext;
}
// GET api/values
public List<gj> Get()
{
return _coreDbContext.Set<gj>().ToList();
}
<p> </p>
<p>3.完成配置运行项目</p>
<p>测试如下</p>
<p><div align="center"></div></p>
<p> </p>
<p>至此就完成了后端api项目</p>
<p> 前端配置链接 https://www.cnblogs.com/ouyangkai/p/11504928.html</p><br><br/><br/><br/><br/><br/>来源:<a href="https://www.cnblogs.com/ouyangkai/archive/2019/09/11/11504279.html" target="_blank">https://www.cnblogs.com/ouyangkai/archive/2019/09/11/11504279.html</a>
页:
[1]