Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I launch the web browser after starting my ASP.NET Core application?

Tags:

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.

like image 445
Justin Helgerson Avatar asked Jul 25 '16 20:07

Justin Helgerson


People also ask

How do I open .NET Core web application?

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).

Does ASP net run in browser?

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#.


1 Answers

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          }     } } 
like image 199
Gerardo Grignoli Avatar answered Nov 01 '22 17:11

Gerardo Grignoli