Skip to content

Commit

Permalink
Support starting GxNetCoreStartup.exe with double click on web\bin di…
Browse files Browse the repository at this point in the history
…rectory (#758)

* Support starting GxNetCoreStartup.exe with double click on web\bin directory.

* Setup ContentRoot also so appsettings.json is found.
  • Loading branch information
claudiamurialdo authored Feb 6, 2023
1 parent e442f6e commit e37109b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<Startup>()
.UseContentRoot(Startup.LocalPath)
.Build();

public static IWebHost BuildWebHostPort(string[] args, string port)
Expand All @@ -82,8 +89,17 @@ static IWebHost BuildWebHostPort(string[] args, string port, string schema)
.ConfigureLogging(logging => logging.AddConsole())
.UseUrls($"{schema}://*:{port}")
.UseStartup<Startup>()
.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
Expand All @@ -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";
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e37109b

Please sign in to comment.