I have a ASP.NET Core application that will be used as a client by multiple users. In other words, it will not be hosted on a central server and they will run the published executable anytime they need to use the application.
In the Program.cs
file there's the following:
var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run();
I would like the default web browser to automatically open to avoid a redundant step of the user having to open the browser and manually put in the http://localhost:5000
address.
What would be the best way to achieve this? Calling Program.Start
after the call to Run()
won't work because Run
blocks the thread.
Run the ASP.NET Core Application: To run this web application, click on IIS Express or press F5 (with Debug) or Ctrl + F5 (without Debug).
Alternatively, you can design your ASP.NET Web pages to run on a specific browser such as Microsoft Internet Explorer 6 and take advantage of browser-specific features. Compatible with any language supported by the . NET common language runtime, including Microsoft Visual Basic and Microsoft Visual C#.
You have two different problems here:
Thread Blocking
host.Run()
indeed blocks the main thread. So, use host.Start()
(or await StartAsync
on 2.x) instead of host.Run()
.
How to start the web browser
If you are using ASP.NET Core over .NET Framework 4.x, Microsoft says you can just use:
Process.Start("http://localhost:5000");
But if you are targeting multiplatform .NET Core, the above line will fail. There is no single solution using .NET Standard
that works on every platform. The Windows-only solution is:
System.Diagnostics.Process.Start("cmd", "/C start http://google.com");
Edit: I created a ticket and a MS dev answered that as-of-today, if you want a multi platform version you should do it manually, like:
public static void OpenBrowser(string url) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); // Works ok on windows } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Process.Start("xdg-open", url); // Works ok on linux } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Process.Start("open", url); // Not tested } else { ... } }
All together now:
using System.Threading; public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Start(); OpenBrowser("http://localhost:5000/"); } public static void OpenBrowser(string url) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Process.Start("xdg-open", url); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Process.Start("open", url); } else { // throw } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With