一、创建项目
1.1.创建新项目:打开开发工具>创建新项目>搜索API>选择C#语言的ASP.NET Core Web API
1.2.配置新项目:**自定义项目信息以及存储路径
1.3.其他信息:这里框架必须选择.NET 6.0,其他配置默认勾选即可,也可以根据自己的需求进行更改
1.4.创建完成之后就得到了一个最基础的Core6项目框架代码
Program.cs就是该项目程序的配置,在Core6中Program.cs 集成了 Startup.cs 的配置
项目结构分析,.net6中删除了startup.cs,整合到了Program.cs
创建Controller
可以先将系统默认创建的WeatherForecastController.cs和WeatherForecast.cs进行删除,然后在controller文件夹新建一个控制器,命名:UserController.cs
在UserController中增加一个Hello的get方式的接口方法,返回”你好世界”,通过请求api/user/hello来获取该接口的结果
using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;namespace WebApplication1.Controllers{[Route("api/[controller]")][ApiController]public class UserController : ControllerBase{[HttpGet("[action]")]public IActionResult SayHello(){return new JsonResult("Hello World");}}}
点击运行后,开发工具会自动编译后启动。
会生成一个exe文件并自动运行,运行成功后会打开浏览器进行查看
总结
其实项目的创建以及控制器的创建和core3是一致,只是.Net Core 6 给我的感觉就是比以往的版本更精简。可以将Swagger在创建项目时直接生成,而不需要手动的去配置
建立数据库连接(SQLServer)
二、读取appsettings.json
使用Nuget安装
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration.Binder
根据自己的开发环境选择合适的版本进行安装
2.1、修改Program.cs
①在var builder = WebApplication.CreateBuilder(args);前增加
using WebApplication1.Helper;//获取当前环境:提供两种方法//方法一//① var env = Environment.GetEnvironmentVariables();//从env中获取环境//② var envName = env["ASPNETCORE_ENVIRONMENT"];//方法二//var env2 = WebApplication.CreateBuilder().Environment.EnvironmentName;//读取配置文件//IConfiguration configuration = new ConfigurationBuilder().AddJsonFile($"appsettings.{env2}.json").Build();//读取配置文件IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddControllers();// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbucklebuilder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();var app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){app.UseSwagger();app.UseSwaggerUI();}app.UseHttpsRedirection();app.UseAuthorization();app.MapControllers();app.Run();
②在var builder = WebApplication.CreateBuilder(args);后增加
using WebApplication1.Helper;//获取当前环境:提供两种方法//方法一//① var env = Environment.GetEnvironmentVariables();//从env中获取环境//② var envName = env["ASPNETCORE_ENVIRONMENT"];//方法二//var env2 = WebApplication.CreateBuilder().Environment.EnvironmentName;//读取配置文件//IConfiguration configuration = new ConfigurationBuilder().AddJsonFile($"appsettings.{env2}.json").Build();//读取配置文件IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();var builder = WebApplication.CreateBuilder(args);builder.Services.AddSingleton(new Appsettings(configuration));// Add services to the container.builder.Services.AddControllers();// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbucklebuilder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();var app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){app.UseSwagger();app.UseSwaggerUI();}app.UseHttpsRedirection();app.UseAuthorization();app.MapControllers();app.Run();
③读取
appsettings.json 代码
{"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"AllowedHosts": "*","AppSettings": {//数据库连接字符串"ConnectionString": "Server=127.0.0.1;User Id=用户id;Password=密码;Database=数据库名称;"}}
1、新建控制器,配置api路由
3 编写代码
配置数据库连接
在项目根目录下的 appsettings.json 文件中配置数据库连接信息。
2、打开配置文件appsettings.json,配置连接SQLserver的账号和密码。
{/*"ConnectionStrings": {"DefaultConnection": "Server=localhost;Database=MyInventorySystem;User ID=sa;Password=123456;Trusted_Connection=True;"},*/// "Constr": "Data Source=127.0.0.1;Database=TestBase;Uid=sa;Pwd=123", //添加数据库的连接地址"Constr": "Data Source=aliworks.cn,57468;Database=DBAliTemp;Uid=test_db;Pwd=alibaba123456", //添加数据库的连接地址"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"AllowedHosts": "*"}