一:引用关系图
要写一个项目首先离不开的就是一个清晰的流程图,当然我这里很简单。
上诉完成后打开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"]; } } }
因为我采用了5个过滤器所以在上诉代码上addmvc中添加了5个过滤器。
上面的代码我都有注解,都很简单,欢迎学习交流。
时间: 2024-10-09 03:12:17