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

}

}

作者:蒋光洵

相关推荐
ch.ju几秒前
Java Programming Chapter 4——Multi-level inheritance
java·开发语言
Molesidy3 分钟前
【Linux】【C++】Linux下的C++编程以及基于GDB的VSCode的C++调试
开发语言·c++
techdashen4 分钟前
用 Rust 真正发出 Ping:FFI 类型、newtype 与 MaybeUninit
开发语言·后端·rust
塵觴葉8 分钟前
基于Lua协程的简单任务管理
开发语言·lua
liulilittle9 分钟前
甲骨文云中国大陆定向 QoS 原理及绕过解决方案
服务器·开发语言·网络·计算机网络·oracle·通信·qos
iCxhust11 分钟前
C# 生成命令行程序 将hex格式烧录程序转换成bin烧录格式
开发语言·汇编·单片机·嵌入式硬件·c#·微机原理
Mortalbreeze11 分钟前
C++11类的新特性:移动语义、default、delete、override详解
开发语言·c++
xiaoshuaishuai812 分钟前
C# 封装与继承
开发语言·c#
FL162386312914 分钟前
基于C#winform使用纯opencv部署ppocrv5和ppocrv6的onnx模型进行OCR文件检测识别
opencv·c#·ocr
星辰_mya22 分钟前
限流、漏斗桶和令牌桶的区别
java·开发语言·面试·架构·高并发