2016-07-25 271 views
9

我有一個ASP.NET Core應用程序將被多個用戶用作客戶端。換句話說,它不會託管在中央服務器上,並且它們將在需要使用應用程序的任何時候運行已發佈的可執行文件。啓動我的ASP.NET Core應用程序後,如何啓動Web瀏覽器?

Program.cs文件有以下幾點:

var host = new WebHostBuilder() 
    .UseKestrel() 
    .UseContentRoot(Directory.GetCurrentDirectory()) 
    .UseIISIntegration() 
    .UseStartup<Startup>() 
    .Build(); 

host.Run(); 

我想默認的Web瀏覽器自動打開,以避免不必打開瀏覽器並手動將在http://localhost:5000地址的用戶的多餘步驟。

什麼是最好的方法來實現這一目標?在撥打Run()後調用Program.Start將不起作用,因爲Run會阻塞該線程。

回答

10

您這裏有兩個不同的問題:

線程阻塞

host.Run()的確塊主線程。因此,使用host.Start()(或2.17上的await StartAsync)而不是host.Run()

如何啓動網頁瀏覽器

如果是在.NET框架4.x中使用ASP.NET核心,微軟says你可以使用:

Process.Start("http://localhost:5000"); 

但如果你是針對多平臺的.NET Core,上述行將失敗。沒有單一的解決方案使用.NET Standard適用於每個平臺。在Windows的唯一的解決辦法是:

System.Diagnostics.Process.Start("cmd", "/C start http://google.com"); 

編輯:我創建了一個ticket和MS dev的回答是,今天的-,如果你想有一個多平臺的版本,你應該做手工,如:

public static void OpenBrowser(string url) 
{ 
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 
    { 
     Process.Start(new ProcessStartInfo("cmd", $"/c start {url}")); // 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 
    { 
     ... 
    } 
} 

總之現在

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("cmd", $"/c start {url}")); 
     } 
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) 
     { 
      Process.Start("xdg-open", url); 
     } 
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) 
     { 
      Process.Start("open", url); 
     } 
     else 
     { 
      // throw 
     } 
    } 
} 
1

您可以在host.Run()之前產生Web瀏覽器進程。這適用於Chrome和其他可能的瀏覽器:

public static void Main(string[] args) 
{ 
    var host = new WebHostBuilder() 
     .UseKestrel() 
     .UseContentRoot(Directory.GetCurrentDirectory()) 
     .UseIISIntegration() 
     .UseStartup<Startup>() 
     .Build(); 

    var browserExecutable = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"; 
    Process.Start(browserExecutable, "http://localhost:5000"); 

    host.Run(); 
} 

在Chrome中的情況下,新窗口將等待服務器旋轉起來,然後連接和顯示應用程序。

根據您系統的配置方式,您可能可以執行Process.Start("http://localhost:5000")來啓動默認瀏覽器,而不是硬編碼到可執行文件的路徑。由於某種原因,這對我不起作用。您也可以將瀏覽器路徑從註冊表中取出或取出配置文件。

1

還有另一個選擇是解決一個IApplicationLifetime對象並在ApplicationStarted上註冊回撥。該事件在主機啓動並正在偵聽時觸發。

public void Configure(IApplicationBuilder app, IApplicationLifetime appLifetime) 
{ 
    appLifetime.ApplicationStarted.Register(() => OpenBrowser(
     app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First())); 
} 

private static void OpenBrowser(string url) 
{ 
    Process.Start(
     new ProcessStartInfo("cmd", $"/c start {url}") 
     { 
      CreateNoWindow = true 
     }); 
} 
相關問題