码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2020-04-26 19:13:57      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:路径   wro   contex   ddl   jscon   mamicode   iter   pre   window   

技术图片

 

 

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("}");
        }
    }
}

 

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

标签:路径   wro   contex   ddl   jscon   mamicode   iter   pre   window   

原文地址:https://www.cnblogs.com/yxzs/p/12781274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!