如何在program.cs中包含环境?

问题描述 投票:0回答:1

我正在尝试使用自定义异常处理程序并将其包含在program.cs中

    var app = builder.Build();

    var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

    app.ConfigureExceptionHandler(env);

因为ConfigureExceptionHandler是异常文件:

using System.Net;
using Microsoft.AspNetCore.Diagnostics;

namespace API.Extensions
{
public static class ExceptionMiddleWareExtentions
{
    public static void ConfigureExceptionHandler(this IApplicationBuilder app, IWebHostEnvironment env)
    {
         if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler(
                options => {
                    options.Run(
                        async context =>
                        {
                            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            var ex = context.Features.Get<IExceptionHandlerFeature>();
                            if(ex != null)
                            {
                                await context.Response.WriteAsync(ex.Error.Message);
                            }
                        }
                    );
            });
        }
    }
}

}

它给我错误,环境变量是一个字符串。怎么做?如何使 env 变量可以在函数中使用?

configuration
1个回答
0
投票

我找到了答案:

var env = builder.Environment;
© www.soinside.com 2019 - 2024. All rights reserved.