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

}

}

作者:蒋光洵

相关推荐
天天进步2015几秒前
Python项目-基于Flask的个人博客系统设计与实现(1)
开发语言·python·flask
安然无虞2 分钟前
31天Python入门——第20天:魔法方法详解
开发语言·后端·爬虫·python
QQ__17646198246 分钟前
Matlab安装tdms插件
开发语言·matlab·tdms插件
画个逗号给明天"9 分钟前
C#从入门到精通(5)
开发语言·笔记·c#
赤秀32 分钟前
C++模板初阶
开发语言·c++
橘猫云计算机设计1 小时前
基于JavaWeb的二手图书交易系统(源码+lw+部署文档+讲解),源码可白嫖!
java·开发语言·前端·毕业设计·php
半桔1 小时前
红黑树剖析
c语言·开发语言·数据结构·c++·后端·算法
江烽渔火1 小时前
C++ 多态
开发语言·c++
m0_490240671 小时前
软件自动化测试(1):python+selenium自动化测试环境搭建
开发语言·python·selenium
2401_858286112 小时前
CD21.【C++ Dev】类和对象(12) 流插入运算符的重载
开发语言·c++·算法·类和对象·运算符重载