NET Core 6.0 webapi simple to use + connect to the database

environment

insert image description here

  • ASP.NET core
  • NET core 6.0

Create WebApi and prevent Api conflicts

insert image description here
insert image description here

insert image description here
insert image description here

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebApi1.Controllers
{
    
    
    //[Route("api/[controller]")]默认路径,网络请求路径为api/controller文件名,如果有多个方法则会冲突
    [Route("api/[controller]/[action]")]//网络请求改为api/controller文件名/函数方法名。避免重名
    [ApiController]
    public class ValuesController : ControllerBase
    {
    
    
        /// <summary>
        /// 简单的网络请求测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string WebApiTest()
        {
    
    
            return "Hello World!";
        }
        /// <summary>
        /// 多方法名测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string WebApiTest2()
        {
    
    
            return "Hello World!";
        }
    }
}




insert image description here
how to use

insert image description here

Swagger adds annotations

Inside Program.cs

insert image description here

.......其他代码
builder.Services.AddSwaggerGen(option =>
    {
    
    
        var file = Path.Combine(AppContext.BaseDirectory, "你的项目名称.xml");//修改为你对应的项目名称才行
        //true:显示控制器层注释
        option.IncludeXmlComments(file, true);
        //对action的名称进行排序,如果有多个,就可以看见效果了
        option.OrderActionsBy(o => o.RelativePath);
    }
); 

var app = builder.Build();
.......其他代码

insert image description here
Add corresponding notes

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebApi1.Controllers
{
    
    
    //[Route("api/[controller]")]默认路径,网络请求路径为api/controller文件名,如果有多个方法则会冲突
    [Route("api/[controller]/[action]")]//网络请求改为api/controller文件名/函数方法名。避免重名
    [ApiController]
    public class ValuesController : ControllerBase
    {
    
    
        /// <summary>
        /// 简单的网络请求测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string WebApiTest()
        {
    
    
            return "Hello World!";
        }
        /// <summary>
        /// 多方法名测试
        /// </summary>
        /// <param name="str1">字符串1</param>
        /// <param name="str2">字符串2</param>
        /// <returns></returns>
        [HttpGet]
        public string WebApiTest2(string str1,string str2)
        {
    
    
            return "Hello World!";
        }
    }
}

Effect:
insert image description here

connect to sqlite database

Nuget package

insert image description here

  • Microsoft.EntityFrameworkCore.Sqlite
  • Microsoft.EntityFrameworkCore.Sqlite.Core
  • Newtonsoft.Json: for Json format conversion

the code

insert image description here

using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace WebApi1.DB
{
    
    
    public class ORMContext:DbContext
    {
    
    
        public DbSet<Student> Students {
    
     get; set; }

        public ORMContext()
        {
    
    
            this.Database.EnsureCreated();//如果没有数据库,则自动创建数据库和对应数据表
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
    
    
            //数据库连接字符串
            optionsBuilder.UseSqlite("Data Source=ORM_Sqlite.db");//你的数据库名称
        }

    }
    public class Student
    {
    
    

        [Key]//主键
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]//自动递增
        public int Id {
    
     get; set; }

        public string Name {
    
     get; set; }

        public int Age {
    
     get; set; }

        public string Sex {
    
     get; set; }
    }
}

Add test API

Tips: ORM query syntax is used here

EntityFramework EF commonly used additions, deletions, modification and query statements

  /// <summary>

        /// <summary>
        /// Sqlite测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]

        public List<Student> SqlTest()
        {
    
    
            for(var i = 0; i < 5; i++)
            {
    
    
                _context.Students.Add(new Student()//插入数据库,但是只在缓存中,并没有真正更新
                {
    
    
                    Age = i,
                    Name = "Lily" + i,
                    Sex = "女"
                });
               
            }
           _context.SaveChanges();//和缓存同步,更新数据库

            var res = _context.Students.ToList();//返回所有数据

            return res;

        }

operation result

insert image description here

Guess you like

Origin blog.csdn.net/qq_44695769/article/details/131316827