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

Posted 月下之神

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.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("}");
        }
    }
}

 

以上是关于.net core 在Startup.cs 的Configure方法中扩展 IApplicationBuilder的主要内容,如果未能解决你的问题,请参考以下文章

.net core 使用swagger生成API文档

。Net Core ControllerBase操作方法JSON序列化设置

.NET Core 当前版本

ASP.NET Core 3.1 MVC JWT 登录返回 401

ASP.NET Core 6 如何在启动期间访问配置

ASP.NET CORE 2.2 WEB APP中的会话过期问题