一种全新的webapi框架C#webmvc初步介绍

这个框架分三部分,第一部分数据结构层,第二部分http和业务管理以及sql层,第三部分加密层和工具类。

数据结构层分key和数据长度定义

public class Auth

{

Key

public string Id { get; set; }

MaxLength(50)

public string Username { get; set; }

MaxLength(50)

public string Authtest { get; set; }

}

http和业务管理以及sql层

url定义

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

ApiController

public class AuthController : ControllerBase

{

HttpPost

sql使用linq方式

List<Hack> hacklist = _coreDbContext.Set<Hack>().Where(i => i.Username ==userid).ToList();

中间还有日志定义

logger.Warn

startup.cs

public class Startup

{

public Startup(IConfiguration configuration)

{

Configuration = configuration;

}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)

{

services.AddControllers();

services.AddMemoryCache();

services.AddSingleton<IQRCode, RaffQRCode>();

services.AddMvc();

services.Configure<PictureOptions>(Configuration.GetSection("PictureOptions"));

services.AddTransient<Microsoft.Extensions.Hosting.IHostedService, Job>();

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

if (env.IsDevelopment())

{

app.UseDeveloperExceptionPage();

}

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>

{

endpoints.MapControllers();

});

}

数据库连接选择以及实体类构建

public virtual DbSet<Auth> Auth { get; set; } //创建实体类添加Context中

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

{

if (!optionsBuilder.IsConfigured)

{

var builder = new ConfigurationBuilder()

.SetBasePath(Directory.GetCurrentDirectory())

.AddJsonFile("appsettings.json");

var config = builder.Build();

string database = config"database";

if (database.Equals("mysql"))

optionsBuilder.UseMySQL(RSADecrypt("", config"source"));

if(database.Equals("sqlserver"))

optionsBuilder.UseSqlServer(RSADecrypt("", config"source"));

}

}

作者:蒋光洵

相关推荐
个 人 练 习 生12 分钟前
C++:从零基础到深入了解vector的使用
开发语言·c++·经验分享·学习·程序人生·stl
H.莓飛1 小时前
【C++】【STL】stack_OJ题
服务器·c语言·开发语言·数据结构·c++·算法·visual studio
hudd6681 小时前
JavaEE 初阶 - 文件操作和IO
java·开发语言·java-ee
starzy19902 小时前
深入理解Scala隐式参数:上下文流转
开发语言·python·scala
feixing_fx3 小时前
JavaScript 是什么:从浏览器脚本到通用编程语言
开发语言·前端·javascript·前端框架·ecmascript
惜.己5 小时前
Java结合idea简单使用Playwright
java·开发语言·自动化
利威尔xu5 小时前
PostgreSQL 索引失效 4 类:函数运算让查询全表扫描拖垮性能
开发语言·数据库·postgresql·性能优化·索引优化
ShineWinsu6 小时前
对于Qt:QSpacerItem常见属性以及方法的解析
开发语言·c++·qt·面试·系统架构·客户端·qspaceritem
ShineWinsu9 小时前
对于Qt:QGripLayout常见属性、方法以及QSizePolicy的解析
开发语言·c++·qt·面试·gui·客户端·网格
艾莉丝努力练剑9 小时前
【AI大模型接入SDK】C++的SessionManager 线程安全
java·开发语言·前端·c++·人工智能·学习·安全