一种全新的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"]));

}

}

作者:蒋光洵

相关推荐
hhb_61814 小时前
C#高性能异步编程实战与底层原理深度解析
开发语言·c#
雾岛听蓝14 小时前
Qt操作指南:状态栏、浮动窗口与对话框使用
开发语言·经验分享·笔记·qt
minji...14 小时前
Linux 线程同步与互斥(五) 日志,线程池
linux·运维·服务器·开发语言·c++·算法
beyond谚语15 小时前
反射、特性和依赖注入
c#
兩尛15 小时前
c++面试常问2
开发语言·c++·面试
Rust研习社15 小时前
添加依赖库时的 features 是什么?优雅实现编译期条件编译与模块化开发
开发语言·后端·rust
Tel1992530800415 小时前
ENDAT2.2 协议信号转 SSI /BISS-C转换卡 ENDAT2.2 协议信号转DMC多摩川高速协议转换器 互转卡
c语言·开发语言·网络
Tiger_shl15 小时前
C# 托管对象、非托管对象 讲解
开发语言·c#
HappyAcmen15 小时前
10.常见报错排查与基础调试
开发语言·python