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

2021-04-30 09:27

阅读:386

标签:es2017   2-2   关系   icon   reac   task   wan   pac   sed   

一:引用关系图

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

技术分享

上诉完成后打开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();
        }
        /// 
        /// 加载所有程序集
        /// 
        public void GetAssembly()
        {
            IList ass = new List();
            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;

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

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

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

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

标签:es2017   2-2   关系   icon   reac   task   wan   pac   sed   

原文地址:http://www.cnblogs.com/edna-lzh/p/7805349.html


评论


亲,登录后才可以留言!