.net core 在Startup.cs 的Configure方法中扩展 IApplicationBuilder

仓廪实则知礼节,衣食足则知荣辱。这篇文章主要讲述.net core 在Startup.cs 的Configure方法中扩展 IApplicationBuilder相关的知识,希望能为你提供帮助。

.net core 在Startup.cs 的Configure方法中扩展 IApplicationBuilder

文章图片

【.net core 在Startup.cs 的Configure方法中扩展 IApplicationBuilder】 
 
using System; using System.Collections.Generic; using System.IO; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; namespace Bayantu.Evos.WebApps.GJExhibition.Infrastructure.Extensions { public static class DomainConfigExtensions {public static IApplicationBuilder DomainConfigToJs(this IApplicationBuilder app, IConfiguration configuration) { string directoryPath = @"wwwroot//exhibition//static//js";           //设置文件路径 if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); }// 文件路径 var filePath = directoryPath + + @"//domain.js"; if (File.Exists(filePath)) { ReadWriteContent(filePath, configuration); } else { FileStream fs = new FileStream(filePath, FileMode.CreateNew); fs.Close(); ReadWriteContent(filePath, configuration); }Func< RequestDelegate, RequestDelegate> middleware = next => { return context => { return next(context); }; }; return app.Use(middleware); }private static void ReadWriteContent(string filePath, IConfiguration configuration) { StreamWriter sw = new StreamWriter(filePath); JsContent(sw, configuration); sw.Flush(); sw.Close(); }public static void JsContent(StreamWriter sw, IConfiguration configuration) { sw.WriteLine("window.domainConfig = {"); sw.WriteLine($"\'NewFileDomain\': \'{configuration["DomainConfig:NewFileDomain"]}\',"); sw.WriteLine("}"); } } }

 

    推荐阅读