From e37109b37bab2544dc3a461cb5b97c56be1b65e2 Mon Sep 17 00:00:00 2001 From: claudiamurialdo <33756655+claudiamurialdo@users.noreply.github.com> Date: Mon, 6 Feb 2023 17:19:12 -0300 Subject: [PATCH] Support starting GxNetCoreStartup.exe with double click on web\bin directory (#758) * Support starting GxNetCoreStartup.exe with double click on web\bin directory. * Setup ContentRoot also so appsettings.json is found. --- .../dotnetcore/GxNetCoreStartup/Startup.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs b/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs index 7b6531cb9..99cac5608 100644 --- a/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs +++ b/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs @@ -50,6 +50,11 @@ public static void Main(string[] args) if (args.Length > 3 && Uri.UriSchemeHttps.Equals(args[3], StringComparison.OrdinalIgnoreCase)) schema = Uri.UriSchemeHttps; } + else + { + LocatePhysicalLocalPath(); + + } if (port == DEFAULT_PORT) { BuildWebHost(null).Run(); @@ -66,10 +71,12 @@ public static void Main(string[] args) Console.Read(); } } + public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddConsole()) .UseStartup() + .UseContentRoot(Startup.LocalPath) .Build(); public static IWebHost BuildWebHostPort(string[] args, string port) @@ -82,8 +89,17 @@ static IWebHost BuildWebHostPort(string[] args, string port, string schema) .ConfigureLogging(logging => logging.AddConsole()) .UseUrls($"{schema}://*:{port}") .UseStartup() + .UseContentRoot(Startup.LocalPath) .Build(); } + private static void LocatePhysicalLocalPath() + { + string startup = FileUtil.GetStartupDirectory(); + string startupParent = Directory.GetParent(startup).FullName; + if (startup == Startup.LocalPath && !File.Exists(Path.Combine(startup, Startup.APP_SETTINGS)) && File.Exists(Path.Combine(startupParent, Startup.APP_SETTINGS))) + Startup.LocalPath = startupParent; + } + } public static class GXHandlerExtensions @@ -108,6 +124,7 @@ public class Startup const long DEFAULT_MAX_FILE_UPLOAD_SIZE_BYTES = 528000000; public static string VirtualPath = string.Empty; public static string LocalPath = Directory.GetCurrentDirectory(); + internal static string APP_SETTINGS = "appsettings.json"; const string UrlTemplateControllerWithParms = "controllerWithParms"; const string RESOURCES_FOLDER = "Resources"; @@ -348,7 +365,7 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos OnPrepareResponse = s => { var path = s.Context.Request.Path; - if (path.HasValue && path.Value.IndexOf("/appsettings.json", StringComparison.OrdinalIgnoreCase)>=0) + if (path.HasValue && path.Value.IndexOf($"/{APP_SETTINGS}", StringComparison.OrdinalIgnoreCase)>=0) { s.Context.Response.StatusCode = 401; s.Context.Response.Body = Stream.Null;