.NET 6 发布时配置开发模式

项目发布后初期往往有各种问题,导致部署后我们看不到详细错误,所以我们发布的时候需要把项目模式设置为开发者模式,这样我们就能看到详细错误了。

.NET 5已经之前版本是这么配置的

 public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>().UseEnvironment("Development").ConfigureLogging((hostingContext, logging) =>
                    {
                        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                        logging.AddConsole();
                    });
                });
    }

  

.NET 6没有Startup了,所以本次项目我也想这么配置发现直接设置builder.Host.UseEnvironment("Development");报错无法运行,

最后在github上摘到了答案,参考文章链接我放在了文章最后。

正常设置如下:

using System.Configuration;

//var builder = WebApplication.CreateBuilder(args);

//用 WebApplicationOptions 代替args var builder = WebApplication.CreateBuilder(new WebApplicationOptions { ApplicationName = typeof(Program).Assembly.FullName, ContentRootPath = Directory.GetCurrentDirectory(), EnvironmentName = Environments.Development, Args = args }); // Add services to the container. builder.Services.AddControllersWithViews(); builder.Logging.AddJsonConsole(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();

参考:https://github.com/dotnet/aspnetcore/issues/36876

 

posted @ 2022-01-26 10:09  麦兜很乖  阅读(746)  评论(0编辑  收藏  举报