.NET CORE2.0後臺管理系統(一)配置API

一:引用關係圖

要寫一個項目首先離不開的就是一個清晰的流程圖,固然我這裏很簡單。json

上訴完成後打開api下的Startup.cs文件,由於我是配置好了所在我直接上傳代碼而後介紹一下:api

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個過濾器。跨域

上面的代碼我都有註解,都很簡單,歡迎學習交流。mvc

相關文章
相關標籤/搜索