I have a very simple program which I want to be able to use multiple user interfaces with. One of those interfaces is going to be an asp netCore 3.1* web app.
For example, if the console application is launched with no parameters and the detected platform is Linux, I would like to start an AspNetCore project and launch a browser which points to the appropriate URL. Alternatively, if the console application is launched with parameters I would like the input and output to continue in a console window. I would also like to leave the possibility for other user interface types.
The result I want is that if certain conditions are met, a factory creates a web app project.
My basic approach so far has to been
WebUi.ProgramWebUi webUi = new ProgramWebUi();
Where ProgramWebUi is the class name in the Program.cs file in an aspWebApp project. This seems to create the project correctly however I also want it to launch the site using the systems default browser.
I have seen approaches for this in Windows. Those approaches being either
Process.Start("Url-Here");
Unfortunately, this doesn't work as it tries to launch an application with the name of the URL. I have also seen others for windows which involved registry lookups. However, I would like this to work cross-platform.
Additionally, even when I manually add a path I do not seem to be able to get
Process.Start("Application Name here");
to work as expected. Even when launching something in the same working directly which is owned by the same user I am getting exceptions telling me permission is denied.
All help gratefully received.
The approach I have found which works for this is to start with an asp app, which is at its core essentially a console application anyway. Then in the program.cs (or whichever class holds your startup) remove the line
CreateHostBuilder(args).Build().Run();
and its associated entry
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
Add these to another class, which can be part of a factory pattern. This way, you can choose whether actually to start the WebUI or not. This approach also allows easy launching of multiple consoles(because it's type is a console app).
When it comes to launching a browser along with the webserver, the approach between operating systems varies. I found lots of articles stating to use Process.Start("URL-here") however I could not make this work in Linux or Windows. In Windows 10, you have to look up the information in the registry. I found a working solution for this bit here . Then use
Process process = new Process();
process.StartInfo.FileName = browserPath;
process.StartInfo.Arguments = "your-url";
process.Start();
For Linux, I tried using a similar approach using xdg-open. However, this wouldn't launch without the full path, which can vary based on distribution. Instead, the answer is to create an executable bash script and use:
Process process = new Process();
process.StartInfo.FileName = "myBashScript.sh";
process.Start();
The bash script is elementary, just two lines of code.
#!/usr/bin/env bash
xdg-open "your-url-here/"
mac users will need (this is based on reading, I do not have a Mac actually to test this)
#!/usr/bin/env bash
open "your-url-here/"
Pulling all this together. Immediately before you run the
CreateHostBuilder(args).Build().Run();
Launch a process to start the browser and add a short thread sleep to give the webserver time to initialise. Finally, get the environment and launch accordingly.
System.Threading.Thread.Sleep(5000);
Process process = new Process();
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
process.StartInfo.FileName = "./BrowserLaunch/BrowserLaunchLinux.sh";
}
else if (Environment.OSVersion.Platform == PlatformID.MacOSX)
{
process.StartInfo.FileName = "./BrowserLaunchBrowserLaunchMacOSx.sh";
}
else if (Environment.OSVersion.Platform == PlatformID.Win32Windows ||
Environment.OSVersion.Platform == PlatformID.Win32NT)
{
string browserPath = GetPathToDefaultBrowser();
process.StartInfo.FileName = browserPath;
process.StartInfo.Arguments = "your-url";
}
else
{
//do something else
}
process.Start();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.