.NET CORE2.0后台管理系统(一)配置API

一:引用关系图

要写一个项目首先离不开的就是一个清晰的流程图,当然我这里很简单。

上诉完成后打开api下的Startup.cs文件,因为我是配置好了所在我直接上传代码然后介绍一下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;
using System.Runtime.Loader;
using System.Reflection;
using KilyCore.Util.FilterGroup;
using KilyCore.Configure;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Web;
using KilyCore.Util.ApplicationService.DependencyIdentity;

namespace KilyCore.Api
{
    public class Startup
    {
        public IEngine Engine { get; private set; }
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
            GetAssembly();
            GetConfiger();
            Engine = EngineExtension.Context;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(option =>
            {
                option.Filters.Add(typeof(AuthorizationFilter));
                option.Filters.Add(typeof(ResourceFilter));
                option.Filters.Add(typeof(ActionFilter));
                option.Filters.Add(typeof(ExceptionFilter));
                option.Filters.Add(typeof(ResultFilter));
                option.RespectBrowserAcceptHeader = true;
            });
            //添加跨域
            services.AddCors(option=>{
                option.AddPolicy("KilyCore", builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
            });
            //添加Session
            services.AddSession();
            return Engine.ServiceProvider(services);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger)
        {
            //Nlog
            logger.AddNLog();
            env.ConfigureNLog("Nlog.config");
            //设置全局跨域
            app.UseCors("KilyCore");
            //启用Session
            app.UseSession();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
        /// <summary>
        /// 加载所有程序集
        /// </summary>
        public void GetAssembly()
        {
            IList<Assembly> ass = new List<Assembly>();
            var lib = DependencyContext.Default;
            var libs = lib.CompileLibraries.Where(t => !t.Serviceable).Where(t => t.Type != "package").ToList();
            foreach (var item in libs)
            {
                Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(item.Name));
                ass.Add(assembly);
            }
            Configer.Assembly = ass;

        }
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public void GetConfiger()
        {
            Configer.ConnentionString = Configuration.GetConnectionString("ConnectionString");
            Configer.RedisConnectionString = Configuration["RedisConnectionString:host"];
            Configer.ApiKey = Configuration["Key:ApiKey"];
        }
    }
}
View Code

因为我采用了5个过滤器所以在上诉代码上addmvc中添加了5个过滤器。

上面的代码我都有注解,都很简单,欢迎学习交流。

转载于:https://www.cnblogs.com/edna-lzh/p/7805349.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenAuth.Core是一个.Net Core快速应用开发框架、好用的权限工作流系统。基于经典领域驱动设计的权限管理及快速开发框架,源于Martin Fowler企业级应用开发思想及最新技术组合(IdentityServer、EF core、Quartz、AutoFac、WebAPI、Swagger、Mock、NUnit、VUE、Element-ui等)。已成功在docker/jenkins中实施。核心模块包括:组织机构、角色用户、权限授权、表单设计、工作流等。它的架构精良易于扩展,是中小企业的首选。 OpenAuth.Core特点: 1、支持.net core sdk 3.1.100 2、超强的自定义权限控制功能,可灵活配置用户、角色可访问的数据权限。请参考:通用权限设计与实现 3、完整的字段权限控制,可以控制字段可见及API是否返回字段值 4、可拖拽的表单设计 5、可视化流程设计 6、基于Quartz.Net的定时任务控制,可随时启/停,可视化配置Cron表达式功能 7、基于CodeSmith的代码生成功能,可快速生成带有头/明细结构的页面 8、支持sqlserver、mysql数据库,理论上支持所有数据库 9、集成IdentityServer4,实现基于OAuth2的登录体系 10、建立三方对接规范,已有系统可以无缝对接流程引擎 11、前端采用vue + layui + elementUI + ztree + gooflow + leipiformdesign 12、后端采用.net core +EF core+ autofac + quartz +IdentityServer4 + nunit + swagger 13、设计工具PowerDesigner + Enterprise Architect 系统工程结构: 1、Infrastructure 通用工具集合 2、OpenAuth.Repository 系统仓储层,用于数据库操作 3、OpenAuth.App 应用层,为界面提供接口 4、OpenAuth.Mvc Web站点 5、OpenAuth.WebApi 为企业版或其他三方系统提供接口服务 6、OpenAuth.Identity 基于IdentityServer4的单点登录服务   OpenAuth.Core 更新日志: v3.2 增加在swagger界面查看接口调用时间及SQL执行时间;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值